文件系统浏览

1.文件与目录管理:copeFile,MoveFile,deleteFile,Createdirectory,removedirectory。

当复制一个大文件时,等待copyFile函数返回可能会让用户因无法预测时间长短而不安。从WinCE5开始支持copyFileex。

BOOL CopyFileEx(LPCTSTR lpExistingFileName,LPCTSTR lpNewFileName,LPPROGRESS_ROUTINE lpProgressRoutine,LPVOID lpData,LPBOOL pbCancel,DWORD dwCopyFlags);

This function copies an existing file to a new file.

BOOL CopyFileEx(
  LPCTSTR lpExistingFileName, 
  LPCTSTR lpNewFileName,
  LPPROGRESS_ROUTINE lpProgressRoutine,
  LPVOID lpData,
  LPBOOL pbCancel,
  DWORD dwCopyFlags
); 
Parameters
lpExistingFileName
[in] Pointer to a null-terminated string that specifies the name of an existing file.
lpNewFileName
[in] Pointer to a null-terminated string that specifies the name of the new file.
lpProgressRoutine
[in] Address of a callback function that is called each time another portion of the file has been copied. This parameter can be NULL.
lpData
[in] Argument to be passed to the callback function. This parameter can be NULL.
pbCancel
[in] If this parameter is set to TRUE during the copy operation, the operation is canceled. Otherwise, the copy operation continues to completion.
dwCopyFlags
[in] Flags that specify how the file is to be copied. This parameter can be set to any combination of the following values.

Value
Description

COPY_FILE_ALLOW_DECRYPTED_DESTINATION
An attempt to copy an encrypted file will succeed even if the destination copy cannot be encrypted.

COPY_FILE_FAIL_IF_EXISTS
The copy operation fails immediately if the target file already exists.

COPY_FILE_RESTARTABLE
Progress of the copy is tracked in the target file in case the copy fails. The failed copy can be restarted at a later time by specifying the same values for lpExistingFileName and lpNewFileName as those used in the call that failed.

Return Values

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

This function fails with ERROR_ACCESS_DENIED if the destination file already exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_READONLY set.

If lpProgressRoutine returns PROGRESS_CANCEL due to the user canceling the operation, CopyFileEx will return zero and GetLastError will return ERROR_REQUEST_ABORTED. In this case, the partially copied destination file is deleted.

If lpProgressRoutine returns PROGRESS_STOP due to the user stopping the operation, CopyFileEx will return zero and GetLastError will return ERROR_REQUEST_ABORTED. In this case, the partially copied destination file is left intact.

Requirements

OS Versions: Windows CE 5.0 and later
Header: Winbase.h.
Link Library: Coredll.lib.

 

CopyProgressRoutine Callback Function

An application-defined callback function used with the CopyFileEx, MoveFileTransacted, and MoveFileWithProgress functions. It is called when a portion of a copy or move operation is completed. The LPPROGRESS_ROUTINE type defines a pointer to this callback function. CopyProgressRoutine is a placeholder for the application-defined function name.

DWORD CALLBACK CopyProgressRoutine(
  [in]                 LARGE_INTEGER TotalFileSize,
  [in]                 LARGE_INTEGER TotalBytesTransferred,
  [in]                 LARGE_INTEGER StreamSize,
  [in]                 LARGE_INTEGER StreamBytesTransferred,
  [in]                 DWORD dwStreamNumber,
  [in]                 DWORD dwCallbackReason,
  [in]                 HANDLE hSourceFile,
  [in]                 HANDLE hDestinationFile,
  [in]                 LPVOID lpData
);
Parameters
TotalFileSize

The total size of the file, in bytes.

TotalBytesTransferred

The total number of bytes transferred from the source file to the destination file since the copy operation began.

StreamSize

The total size of the current file stream, in bytes.

StreamBytesTransferred

The total number of bytes in the current stream that have been transferred from the source file to the destination file since the copy operation began.

dwStreamNumber

A handle to the current stream. The first time CopyProgressRoutine is called, the stream number is 1.

dwCallbackReason

The reason that CopyProgressRoutine was called. This parameter can be one of the following values.

Value
Meaning

CALLBACK_CHUNK_FINISHED

Another part of the data file was copied.

CALLBACK_STREAM_SWITCH

Another stream was created and is about to be copied. This is the callback reason given when the callback routine is first invoked.

hSourceFile

A handle to the source file.

hDestinationFile

A handle to the destination file

lpData

Argument passed to CopyProgressRoutine by CopyFileEx, MoveFileTransacted, or MoveFileWithProgress.

Return Value

The CopyProgressRoutine function should return one of the following values.

Return code
Description

PROGRESS_CONTINUE

Continue the copy operation.

PROGRESS_CANCEL

Cancel the copy operation and delete the destination file.

PROGRESS_STOP

Stop the copy operation. It can be restarted at a later time.

PROGRESS_QUIET

Continue the copy operation, but stop invoking CopyProgressRoutine to report progress.

Remarks

An application can use this information to display a progress bar that shows the total number of bytes copied as a percent of the total file size.

Requirements

Client

Requires Windows Vista, Windows XP, or Windows 2000 Professional.

Server

Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.

Header

Declared in WinBase.h; include Windows.h.

 

 

2.移动文件夹 MoveFile();注意目标文件必须不存在。移动文件可以发生在对象存储中,也可以是从对象存储到外部存储器中,或是从外部驱动器到对象存储中。MoveFile也可以用来重命名文件。在这种情况下,源目录名和目标目录名仍然相同,只是文件名有所改变。

MoveFile也可以以相同的方式用来移动或是重命名目录。这个操作远比调用CopyFile快。MoveFile唯一的限制是无法将一个目录从一个卷移动到另一个卷上。在这种情况下,需要用CopyFile或CopyFileEx()。

3.删除文件;DeleteFile();为了成功删除,文件不能是被打开的。

你可以用CreateDirectory()来创建目录。RemoveDirectory()来删除目录。

4.创建临时文件;你可能需要时不时的创建一个临时文件。如何挑选一个独一无二的文件名呢?可以用以下函数让windows为临时文件选一个名字:

UINT GetTempFileName(LPCTSTR lpPathName,LPCTSTR lpPrefixString,UINT uUnique,LPTSTR lpTempFileName);

5.查找文件;搜索是在每个目录的基础上使用通配符文件名模板来完成的。

在一个目录中搜索,首先将一个文件名模板传递给FindFirstFile。

HANDLE FindFirstFile(
LPCTSTR lpFileName, 
LPWIN32_FIND_DATA lpFindFileData 
); 
文件名模板中的通配符包括?和* ,问号表示任何一个字符均可替换这个位置上的问号。*表示任何数量的字符均
可替换这个位置上的*号。(?代表一个字符,*代表多个字符)
Parameters
lpFileName
[in] Pointer to a null-terminated string that specifies a valid directory or path and filename, which can contain wildcard characters (* and ?).

There is a default string size limit for paths of MAX_PATH characters. This limit is related to how the FindFirstFile function parses paths.

lpFindFileData
[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory.
Return Values

A search handle, used in a subsequent call to FindNextFile or FindClose, indicates success. INVALID_HANDLE_VALUE indicates failure. To get extended error information, call GetLastError.

The FindFirstFile function opens a search handle and returns information about the first file whose name matches the specified pattern. Once the search handle is established, you can use the FindNextFile function to search for other files that match the same pattern. When the search handle is no longer needed, close it by using the FindClose function.

This function searches for files by name only; it cannot be used for attribute-based searches.

要查找下一个文件,调用

BOOL FindNextFile( 
HANDLE hFindFile, 
LPWIN32_FIND_DATA lpFindFileData 
); 
This function continues a file search from a previous call to the FindFirstFile function
Parameters
hFindFile
[in] Search handle returned by a previous call to the FindFirstFile function.
lpFindFileData
[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory. The structure can be used in subsequent calls to FindNextFile to refer to the found file or directory.
Return Values

Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError. If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.

Remarks

The FindNextFile function searches for files by name only; it cannot be used for attribute-based searches.

当完成了查找,必须调用这个函数FindClose();以FindFirstFile返回的句柄作为参数。如果FindFirstFile返回INVALID_HANDLE_VALUE,你不能调用FindClose。

以下一小段代码包含了查找操作的整个过程。这段代码计算了windows目录下的所有文件的总大小。

WIN32_FIND_DATA fd;

HANDLE hFind;

INT nTotalSize = 0;

//start search for all files in the windows directory

hFind = FindFirstFile(TEXT(“//windows//*.*”),&fd);

//if a file was found ,hFind will be valid;

if(hFind != INVALID_HANDLE_VALUE)

{

do

{

//if found file is not a directory ,add its size to the total.(assume that the totoal size is less than 2 GB)

if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))// it is  a file ,not a directory

nTotalSize + = fd.nFileSizeLow;

}while(FindNextFile(hFind,&fd));

FindClose(hFind);

}

 

 

还有一个更强大版本的FindxxxFile系列的API,叫做FindFirstFileEx。这个函数的优点在于附加了只枚举目录甚至是枚举当前正在运行的设备驱动的功能。

This function searches a directory for a file whose name and attributes match those specified in the function call.

HANDLE FindFirstFileEx(
LPCTSTR lpFileName, 
FINDEX_INFO_LEVELS fInfoLevelId, 
LPVOID lpFindFileData, 
FINDEX_SEARCH_OPS fSearchOp, 
LPVOID lpSearchFilter, 
DWORD dwAdditionalFlags 
);
Parameters
lpFileName
[in] Points to a null-terminated string that specifies a valid directory or path and filename, which can contain wildcard characters (* and ?).
fInfoLevelId
[in] Specifies a FINDEX_INFO_LEVELS enumeration type that gives the information level of the returned data.
lpFindFileData
[out] Pointer to the file data. The pointer type is determined by the level of information specified in the fInfoLevelId parameter.
fSearchOp
[in] Specifies a FINDEX_SEARCH_OPS enumeration type that gives the type of filtering to perform beyond wildcard matching.
lpSearchFilter
[in] If the specified fSearchOp needs structured search information, lpSearchFilter points to the search criteria. At this time, none of the supported fSearchOp values require extended search information. Therefore, this pointer must be NULL.
dwAdditionalFlags
[in] Unsupported; set to NULL.
Return Values

If the function succeeds, the return value is a search handle that can be used in a subsequent call to the FindNextFile or FindClose functions.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

Remarks

The FindFirstFileEx function is provided to open a search handle and return information about the first file whose name matches the specified pattern and attributes. File name searches are not case sensitive.

If the underlying file system does not support the specified type of filtering, other than directory filtering, FindFirstFileEx fails with the error ERROR_NOT_SUPPORTED. The application has to use FINDEX_SEARCH_OPS type FileExSearchNameMatch and perform its own filtering.

Once established, the search handle can be used in the FindNextFile function to search for other files that match the same pattern with the same filtering being performed. When the search handle is no longer needed, it should be closed using the FindClose function.

The following calls to FindFirstFileEx and FindFirstFile are equivalent.

FindFirstFileEx( lpFileName, 
                 FindExInfoStandard, 
                 lpFindData, 
                 FindExSearchNameMatch, 
                 NULL, 
                 0 );
FindFirstFile( lpFileName, lpFindData);
Requirements

Runs on
Versions
Defined in
Include
Link to

Windows CE OS
3.0 and later
Winbase.h
Fsmain.libCoredll.lib

typedef enum _FINDEX_INFO_LEVELS {
FindExInfoStandard 
FindExInfoMaxInfoLevel
} FINDEX_INFO_LEVELS; 
Elements
FindExInfoStandard
The FindFirstFileEx function obtains a standard set of attribute information. The data is returned in a WIN32_FIND_DATA structure.
FindExInfoMaxInfoLevel
Not supported.

FINDEX_SEARCH_OPS

See Also

FindFirstFileEx

Requirements

Runs on
Versions
Defined in
Include
Link to

Windows CE OS
3.0 and later
Winbase.h

This enumeration type defines values that are used with the FindFirstFileEx function to specify the type of filtering to perform.

typedef enum _FINDEX_INFO_LEVELS { 
FindExSearchNameMatch, 
FindExSearchLimitToDirectories, 
FindExSearchLimitToDevices, 
FindExSearchMaxSearchOp 
} FINDEX_INFO_LEVELS; 
Elements
FindExSearchNameMatch
Searches for a file that matches the specified filename. Note that lpSearchFilter parameter of FindFirstFileEx must be NULL when this search operation is used.
FindExSearchLimitToDevices
Returns device names only. Device names are generally accessible through the //./<name> convention. The dwAdditionalFlags parameter of FindFirstFileEx cannot be FIND_FIRST_EX_CASE_SENSITIVE when this search operation is used.
FindExSearchLimitToDirectories
This is an advisory flag. If the file system supports directory filtering, the function searches for a file that matches the specified filename and that is a directory. If the file system does not support directory filtering, this flag is silently ignored.
FindExSearchMaxSearchOp
Not supported.
Remarks

When the FindExSearchLimitToDirectories search operation is used, the lpSearchFilter parameter of FindFirstFileEx must be NULL. If you want directory filtering, use this flag on all file systems, but be sure to examine the file attribute data stored into the *lpFindFileData parameter of FindFirstFileEx to determine whether the function has indeed returned a handle to a directory.

When the FindExSearchLimitToDevices search operation is used, passing in a filter pattern is allowed, but either pass in no path (for example, L"c*" to get all devices starting with a c) or a rooted path (L//c*).

6.区分驱动器与目录

要解决这个问题,必须查看目录的文件属性。如果某个目录实际上是次要存储设备(secondary Storage device)--也就是说,他们存储文件的地方并不是在根文件系统下,它的文件属性中会含有FILE_ATTRIBUTE_TEMPORARY标志。WinCE也用这个属性标志来标明如NETWORK目录等其他“非目录”的目录。

两种方法获得驱动器总空间大小等信息。

This function obtains information about the amount of space available on a disk volume: the total amount of space, the total amount of free space, and the total amount of free space available to the user associated with the calling thread.

BOOL GetDiskFreeSpaceEx( 
LPCWSTR  lpDirectoryName, 
PULARGE_INTEGER   lpFreeBytesAvailableToCaller, 
PULARGE_INTEGER   lpTotalNumberOfBytes, 
PULARGE_INTEGER   lpTotalNumberOfFreeBytes 
);
Parameters
lpDirectoryName
[in] Pointer to a null-terminated string that specifies a directory on the disk of interest. This string can be a UNC name.

If lpDirectoryName is NULL, the GetDiskFreeSpaceEx function obtains information about the object store.

Note that lpDirectoryName does not have to specify the root directory on a disk. The function accepts any directory on the disk.

lpFreeBytesAvailableToCaller
[out] Pointer to a variable to receive the total number of free bytes on the disk that are available to the user associated with the calling thread.
lpTotalNumberOfBytes
[out] Pointer to a variable to receive the total number of bytes on the disk that are available to the user associated with the calling thread.
lpTotalNumberOfFreeBytes
[out] Pointer to a variable to receive the total number of free bytes on the disk.

This parameter can be NULL.

Return Values

Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.

//

This function fills in a STORE_INFORMATION structure with the size of the object store and the amount of free space currently in the object store.

A remote application interface (RAPI) version of this function exists, and it is named CeGetStoreInformation (RAPI).

BOOL GetStoreInformation( 
LPSTORE_INFORMATION lpsi 
);
Parameters
lpsi
[out] Pointer to the STORE_INFORMATION structure to be filled in by this function.
Return Values

TRUE indicates success. FALSE indicates failure. To get extended error information, call the GetLastError function.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值