关于监视文件目录下文件是不是已经存在、并打开
关于监视文件目录下文件是否已经存在、并打开。
我的软件里需要监视一个目录,如果有文件传过来就将其打开,这个传过来的文件有几十兆。在这个功能里沃使用CreateFile()函数,CreateFile( szDataFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0),现在有个问题:这个传过来的文件是从网路的另外一台机器传过来,在整个传输过程中肯定要用一定的时间,那么在调用CreateFile()函数时如果该文件仅仅过来了一部分,我想肯定会出问题的。那么我们在软件设计时如何保证那个传输过来的文件是完整的时候,CreateFile()才开始工作?
------解决方案--------------------
你看一下王艳平的《windows 程序设计》中,有关于目录文件监控的demo的,你自己在网上搜一下,找一下相应的源代码吧
------解决方案--------------------
CreateFile
The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object:
files
pipes
mailslots
communications resources
disk devices (Windows NT only)
consoles
directories (open only)
HANDLE CreateFile(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
// pointer to security attributes
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to
// copy
);
Parameters
lpFileName
Pointer to a null-terminated string that specifies the name of the object (file, pipe, mailslot, communications resource, disk device, console, or directory) to create or open.
If *lpFileName is a path, there is a default string size limit of MAX_PATH characters. This limit is related to how the CreateFile function parses paths.
Windows NT: You can use paths longer than MAX_PATH characters by calling the wide (W) version of CreateFile and prepending "\\?\" to the path. The "\\?\" tells the function to turn off path parsing. This lets you use paths that are nearly 32,000 Unicode characters long. However, each component in the path cannot be more than MAX_PATH characters long. You must use fully-qualified paths with this technique. This also works with UNC names. The "\\?\" is ignored as part of the path. For example, "\\?\C:\myworld\private" is seen as "C:\myworld\private", and "\\?\UNC\tom_1\hotstuff\coolapps" is seen as "\\tom_1\hotstuff\coolapps".
dwDesiredAccess
Specifies the type of access to the object. An application can obtain read access, write access, read-write access, or device query access. This parameter can be any combination of the following values. Value Meaning
0 Specifies device query access to the object. An application can query device attributes without accessing the device.
GENERIC_READ Specifies read access to the object. Data can be read from the file and the file pointer can be moved. Combine with GENERIC_WRITE for read-write access.
GENERIC_WRITE Specifies write access to the object. Data can be written to the file and the file pointer can be moved. Combine with GENERIC_READ for read-write access.
dwShareMode
Set of bit flags that specifies how the object can be shared. If dwShareMode is 0, the object cannot be shared. Subsequent open operations on the object will fail, until the handle is closed.
我的软件里需要监视一个目录,如果有文件传过来就将其打开,这个传过来的文件有几十兆。在这个功能里沃使用CreateFile()函数,CreateFile( szDataFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0),现在有个问题:这个传过来的文件是从网路的另外一台机器传过来,在整个传输过程中肯定要用一定的时间,那么在调用CreateFile()函数时如果该文件仅仅过来了一部分,我想肯定会出问题的。那么我们在软件设计时如何保证那个传输过来的文件是完整的时候,CreateFile()才开始工作?
------解决方案--------------------
你看一下王艳平的《windows 程序设计》中,有关于目录文件监控的demo的,你自己在网上搜一下,找一下相应的源代码吧
------解决方案--------------------
CreateFile
The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object:
files
pipes
mailslots
communications resources
disk devices (Windows NT only)
consoles
directories (open only)
HANDLE CreateFile(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
// pointer to security attributes
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to
// copy
);
Parameters
lpFileName
Pointer to a null-terminated string that specifies the name of the object (file, pipe, mailslot, communications resource, disk device, console, or directory) to create or open.
If *lpFileName is a path, there is a default string size limit of MAX_PATH characters. This limit is related to how the CreateFile function parses paths.
Windows NT: You can use paths longer than MAX_PATH characters by calling the wide (W) version of CreateFile and prepending "\\?\" to the path. The "\\?\" tells the function to turn off path parsing. This lets you use paths that are nearly 32,000 Unicode characters long. However, each component in the path cannot be more than MAX_PATH characters long. You must use fully-qualified paths with this technique. This also works with UNC names. The "\\?\" is ignored as part of the path. For example, "\\?\C:\myworld\private" is seen as "C:\myworld\private", and "\\?\UNC\tom_1\hotstuff\coolapps" is seen as "\\tom_1\hotstuff\coolapps".
dwDesiredAccess
Specifies the type of access to the object. An application can obtain read access, write access, read-write access, or device query access. This parameter can be any combination of the following values. Value Meaning
0 Specifies device query access to the object. An application can query device attributes without accessing the device.
GENERIC_READ Specifies read access to the object. Data can be read from the file and the file pointer can be moved. Combine with GENERIC_WRITE for read-write access.
GENERIC_WRITE Specifies write access to the object. Data can be written to the file and the file pointer can be moved. Combine with GENERIC_READ for read-write access.
dwShareMode
Set of bit flags that specifies how the object can be shared. If dwShareMode is 0, the object cannot be shared. Subsequent open operations on the object will fail, until the handle is closed.