WIN32_FIND_DATA

WIN32_FIND_DATA

在用findfirst()和findnext()函式去查找磁碟檔案時經常使用的一個數據結構WIN32_FIND_DATA的成員變數里包含了以上所有的檔案屬性,因此可以通過這個結構作為獲取和更改檔案屬性的手段。

頭檔案

#include <windows.h>

基本信息

關於檔案的全部屬性信息。總計有以下以下9種:檔案的標題名、檔案的屬性(唯讀、存檔,隱藏等)、檔案的創建時間、檔案的最後訪問時間、檔案的最後修改時間、檔案大小的高位雙字、檔案大小的低位雙字、保留、保留。在這裡只有檔案標題名和檔案的長度可以通過CFile類比較方便的獲得,而對於其他幾種屬性的獲取和設定就無能為力了。

該結構的內容如下:

typedef struct _WIN32_FIND_DATA {

DWORD dwFileAttributes; //檔案屬性

FILETIME ftCreationTime; // 檔案創建時間

FILETIME ftLastAccessTime; // 檔案最後一次訪問時間

FILETIME ftLastWriteTime; // 檔案最後一次修改時間

DWORD nFileSizeHigh; // 檔案長度高32位

DWORD nFileSizeLow; // 檔案長度低32位

DWORD dwReserved0; // 系統保留

DWORD dwReserved1; // 系統保留

TCHAR cFileName[ MAX_PATH ]; // 長檔案名稱

TCHAR cAlternateFileName[ 14 ]; // 8.3格式檔案名稱

} WIN32_FIND_DATA, *PWIN32_FIND_DATA;

可以通過FindFirstFile()函式根據當前的檔案存放路徑查找該檔案來把待操作檔案的相關屬性讀取到WIN32_FIND_DATA結構中去:

WIN32_FIND_DATA ffd ;

HANDLE hFind = FindFirstFile("c:\\test.dat",&ffd);

在使用這個結構時不能手工修改這個結構中的任何數據,結構對於開發人員來說只能作為一個唯讀數據,其所有的成員變數都會由系統完成填寫。在MSDN幫助中可以查找到關於WIN32_FIND_DATA結構的更加詳細的說明。

檔案屬性

File Attribute Constants

File attributes are metadata values stored by the file system on disk and are used by the system and are available to developers via various file I/O APIs. For a list of related APIs and topics, see the See Also section.

Constant/valueDescription
  • FILE_ATTRIBUTE_ARCHIVE32 (0x20)
A file or directory that is an archive file or directory. Applications typically use this attribute to markfiles for backup or removal.存檔類
  • FILE_ATTRIBUTE_COMPRESSED2048 (0x800)
A file or directory that is compressed. For a file, all of the data in the file is compressed. For a directory, compression is the default for newly created files and subdirectories.
  • FILE_ATTRIBUTE_DEVICE64 (0x40)
This value is reserved for system use.驅動類
  • FILE_ATTRIBUTE_DIRECTORY16 (0x10)
The handle that identifies a directory.目錄類
  • FILE_ATTRIBUTE_ENCRYPTED16384 (0x4000)
A file or directory that is encrypted. For a file, all data streams in the file are encrypted. For a directory, encryption is the default for newly created files and subdirectories.
  • FILE_ATTRIBUTE_HIDDEN2 (0x2)
The file or directory is hidden. It is not included in an ordinary directory listing.隱藏
  • FILE_ATTRIBUTE_INTEGRITY_STREAM32768 (0x8000)
The directory or user data stream is configured with integrity (only supported on ReFS volumes). It is not included in an ordinary directory listing. The integrity setting persists with the file if it's renamed. If a file is copied the destination file will have integrity set if either the source file or destination directory have integrity set. Windows Server2008R2, Windows7, Windows Server2008, WindowsVista, Windows Server2003, and WindowsXP:This flag is not supported until Windows Server2012.
  • FILE_ATTRIBUTE_NORMAL128 (0x80)
A file that does not have other attributes set. This attribute is valid only when used alone.普通
  • FILE_ATTRIBUTE_NOT_CONTENT_INDEXED8192 (0x2000)
The file or directory is not to be indexed by the content indexing service.
  • FILE_ATTRIBUTE_NO_SCRUB_DATA131072 (0x20000)
The user data stream not to be read by the background data integrity scanner (AKA scrubber). When set on a directory it only provides inheritance. This flag is only supported on Storage Spaces and ReFS volumes. It is not included in an ordinary directory listing. Windows Server2008R2, Windows7, Windows Server2008, WindowsVista, Windows Server2003, and WindowsXP:This flag is not supported until Windows8 and Windows Server2012.
  • FILE_ATTRIBUTE_OFFLINE4096 (0x1000)
The data of a file is not available immediately. This attribute indicates that the file data is physically moved to offline storage. This attribute is used by Remote Storage, which is the hierarchical storage management software. Applications should not arbitrarily change this attribute.
  • FILE_ATTRIBUTE_READONLY1 (0x1)
A file that is read-only. Applications can read the file, but cannot write to it or delete it. This attribute is not honored on directories. For more information, see You cannot view or change the Read-only or the System attributes of folders in Windows Server 2003, in Windows XP, in Windows Vista or in Windows 7.
  • FILE_ATTRIBUTE_REPARSE_POINT1024 (0x400)
A file or directory that has an associated reparse point, or a file that is a symbolic link.
  • FILE_ATTRIBUTE_SPARSE_FILE512 (0x200)
A file that is a sparse file.
  • FILE_ATTRIBUTE_SYSTEM4 (0x4)
A file or directory that the operating system uses a part of, or uses exclusively.系統檔案
  • FILE_ATTRIBUTE_TEMPORARY256 (0x100)
A file that is being used for temporary storage. File systems avoid writing data back to mass storage ifsufficient cache memory is available, because typically, an application deletes a temporary file after the handleis closed. In that scenario, the system can entirely avoid writing the data. Otherwise, the data is written afterthe handle is closed.臨時檔案
  • FILE_ATTRIBUTE_VIRTUAL65536 (0x10000)
This value is reserved for system use.虛擬檔案(系統專用)

信息獲取

為了更好的保存獲取到的檔案屬性信息,對應於檔案屬性構造一個自定義的FILE_INFO數據結構,獲取的屬性信息可暫存於此:

typedef struct _FILE_INFO {

TCHAR szFileTitle[128]; //檔案的標題名

DWORD dwFileAttributes; //檔案的屬性

FILETIME ftCreationTime; //檔案的創建時間

FILETIME ftLastAccessTime; //檔案的最後訪問時間

FILETIME ftLastWriteTime; //檔案的最後修改時間

DWORD nFileSizeHigh; //檔案大小的高位雙字

DWORD nFileSizeLow; //檔案大小的低位雙字

DWORD dwReserved0; //保留,為0

DWORD dwReserved1; //保留,為0

} FILE_INFO, * PFILE_INFO;

首先用FindFirstFile()函式將檔案屬性獲取到WIN32_FIND_DATA 結構對象FindFileData中去,之後可以用FindClose()將其關閉,並把FindFileData中的有關檔案屬性信息的內容複製到自定義結構FILE_INFO的結構對象FileInfo中備用。下面是關於這部分描述的部分關鍵代碼:

//聲明結構對象

FILE_INFO FileInfo;

WIN32_FIND_DATA FindFileData;

……

//獲取檔案屬性信息

FindClose(FindFirstFile("Test.txt",&FindFileData));

memset(&FileInfo,0,sizeof(FILE_INFO));

……

//將檔案屬性信息保存到FileInfo中備用

strcpy(FileInfo.szFileTitle,myFile.GetFileTitle());

FileInfo.dwFileAttributes = FindFileData.dwFileAttributes;

FileInfo.ftCreationTime = FindFileData.ftCreationTime;

FileInfo.ftLastAccessTime = FindFileData.ftLastAccessTime;

FileInfo.ftLastWriteTime = FindFileData.ftLastWriteTime;

FileInfo.nFileSizeHigh = FindFileData.nFileSizeHigh;

FileInfo.nFileSizeLow = FindFileData.nFileSizeLow;

……

在獲取到檔案的原始屬性信息後既可以原封不動的將屬性重新寫到檔案,也可以對其中某一項或某幾項屬性內容進行修改後再行寫入檔案,從而達到更改檔案屬性的目的。比如可以用SetFileTime()函式設定檔案的創建時間、最近一次訪問時間以及最近一次修改的時間等等:

SetFileTime((HANDLE)destFile.m_hFile, //待寫入的檔案句柄

&FileInfo.ftCreationTime, //檔案的創建時間

&FileInfo.ftLastAccessTime, //檔案最近一次的訪問時間

&FileInfo.ftLastWriteTime); //檔案最近一次的修改時間

也可以用SetFileAttributes() 函式實現對檔案屬性的修改:

SetFileAttributes(FileInfo.szFileTitle,FileInfo.dwFileAttributes);

至於檔案名稱的修改則更加簡單,直接在創建檔案時在CreateFile()或CFile類的成員函式Open里直接對檔案名稱參數進行設定即可。

移動一個檔案,一般可使用:

BOOL WINAPI MoveFile(LPCSTR Existing, LPCSTR Target);

其中 Existing是現有檔案或目錄,Target是目標檔案或目錄。此函式還可以用來對檔案改名。例:

MoveFile("D:\\Temp\\a.txt", "E:\\MyPath\\b.txt"); 將D:\Temp\a.txt移動到E:\MyPath並改名為b.txt

>API中的CopyFile:

BOOL WINAPI CopyFile(LPCSTR oldFileName, LPCSTR newFileName, BOOL failIfExists);

oldFileName: 原始檔案名稱;

newFileName: 目標檔案名稱;

failIfExists: 目標檔案已存在時的處理辦法。TRUE則不覆蓋並返回失敗標記,FALSE則覆蓋。

返回值:TRUE代表成功,FALSE代表失敗。

>貼上又是哪個函式呢?

貼上的功能不是一個函式能夠實現的.對剪貼簿的操作Windows有一系列函式.貼上必須要用的是 OpenClipboard(), GetCipboardData()和CloseClipboard().

拷貝-貼上的對象為文本或其他二進數據(如圖像)時可能還要輔助其他的API.

相關詞條

熱門詞條

聯絡我們