用文件修改时间来判断文件是否修改,遇到问题大家看看

zjl3638656 2006-09-30 01:44:55
刚开始用
BOOL GetFileAttributesEx(
LPCTSTR lpFileName,
GET_FILEEX_INFO_LEVELS fInfoLevelId,
LPVOID lpFileInformation
);
但第二个参数不知道怎么搞都不行
后来用 GetFileTime又没搞定,大家出个主意.
...全文
312 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
an_bachelor 2006-10-08
  • 打赏
  • 举报
回复
SDK的例子:


Obtaining Directory Change Notifications
An application can monitor the contents of a directory and its subdirectories by using change notifications. Waiting for a change notification is similar to having a read operation pending against a directory and, if necessary, its subdirectories. When something changes within the directory being watched, the read operation is completed. For example, an application can use these functions to update a directory listing whenever a file name within the monitored directory changes.


An application can specify a set of conditions that trigger a change notification by using the FindFirstChangeNotification function. The conditions include changes to file names, directory names, attributes, file size, time of last write, and security. This function also returns a handle that can be waited on by using the wait functions. If the wait condition is satisfied, FindNextChangeNotification can be used to provide a notification handle to wait on subsequent changes. The FindCloseChangeNotification function closes the notification handle. However, these functions do not indicate the actual change that satisfied the wait condition.

To retrieve information about the specific change as part of the notification, use the ReadDirectoryChangesW function. This function also enables you to provide a completion routine.

To track changes on a volume, see change journals.



The following example monitors the directory tree for directory name changes. It also monitors a directory for file name changes. The example uses the FindFirstChangeNotification function to create two notification handles and the WaitForMultipleObjects function to wait on the handles. Whenever a directory is created or deleted in the tree, the example should update the entire directory tree. Whenever a file is created or deleted in the directory, the example should refresh the directory.


#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>

void RefreshDirectory(LPTSTR);
void RefreshTree(LPTSTR);

void WatchDirectory(LPTSTR lpDir)
{
DWORD dwWaitStatus;
HANDLE dwChangeHandles[2];
TCHAR lpDrive[4];
TCHAR lpFile[_MAX_FNAME];
TCHAR lpExt[_MAX_EXT];

_tsplitpath(lpDir, lpDrive, NULL, lpFile, lpExt);

lpDrive[2] = (TCHAR)'\\';
lpDrive[3] = (TCHAR)'\0';

// Watch the directory for file creation and deletion.

dwChangeHandles[0] = FindFirstChangeNotification(
lpDir, // directory to watch
FALSE, // do not watch subtree
FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes

if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
ExitProcess(GetLastError());

// Watch the subtree for directory creation and deletion.

dwChangeHandles[1] = FindFirstChangeNotification(
lpDrive, // directory to watch
TRUE, // watch the subtree
FILE_NOTIFY_CHANGE_DIR_NAME); // watch dir. name changes

if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)
ExitProcess(GetLastError());

// Change notification is set. Now wait on both notification
// handles and refresh accordingly.

while (TRUE)
{
// Wait for notification.

dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles,
FALSE, INFINITE);

switch (dwWaitStatus)
{
case WAIT_OBJECT_0:

// A file was created or deleted in the directory.
// Refresh this directory and restart the notification.

RefreshDirectory(lpDir);
if ( FindNextChangeNotification(
dwChangeHandles[0]) == FALSE )
ExitProcess(GetLastError());
break;

case WAIT_OBJECT_0 + 1:

// A directory was created or deleted in the subtree.
// Refresh the tree and restart the notification.

RefreshTree(lpDrive);
if (FindNextChangeNotification(
dwChangeHandles[1]) == FALSE)
ExitProcess(GetLastError());
break;

default:
ExitProcess(GetLastError());
}
}
}

void RefreshDirectory(LPTSTR lpDir)
{
_tprintf(TEXT("Refresh the directory (%s).\n"), lpDir);
}

void RefreshTree(LPTSTR lpDrive)
{
_tprintf(TEXT("Refresh the directory tree (%s).\n"), lpDrive);
}


Send comments about this topic to Microsoft
an_bachelor 2006-10-08
  • 打赏
  • 举报
回复
直接监视文件更改不更好?马上就知道更改了



FindFirstChangeNotification

The FindFirstChangeNotification function creates a change notification handle and sets up initial change notification filter conditions. A wait on a notification handle succeeds when a change matching the filter conditions occurs in the specified directory or subtree. The function does not report changes to the specified directory itself.

This function does not indicate the change that satisfied the wait condition. To retrieve information about the specific change as part of the notification, use the ReadDirectoryChangesW function.


HANDLE FindFirstChangeNotification(
LPCTSTR lpPathName,
BOOL bWatchSubtree,
DWORD dwNotifyFilter
);

Parameters
lpPathName
[in] Pointer to a null-terminated string that specifies the path of the directory to watch.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

Windows Me/98/95: This string must not exceed MAX_PATH characters.
bWatchSubtree
[in] Specifies whether the function will monitor the directory or the directory tree. If this parameter is TRUE, the function monitors the directory tree rooted at the specified directory; if it is FALSE, it monitors only the specified directory.
dwNotifyFilter
[in] Filter conditions that satisfy a change notification wait. This parameter can be one or more of the following values. Value Meaning
FILE_NOTIFY_CHANGE_FILE_NAME Any file name change in the watched directory or subtree causes a change notification wait operation to return. Changes include renaming, creating, or deleting a file name.
FILE_NOTIFY_CHANGE_DIR_NAME Any directory-name change in the watched directory or subtree causes a change notification wait operation to return. Changes include creating or deleting a directory.
FILE_NOTIFY_CHANGE_ATTRIBUTES Any attribute change in the watched directory or subtree causes a change notification wait operation to return.
FILE_NOTIFY_CHANGE_SIZE Any file-size change in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change in file size only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.
FILE_NOTIFY_CHANGE_LAST_WRITE Any change to the last write-time of files in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change to the last write-time only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.
FILE_NOTIFY_CHANGE_SECURITY Any security-descriptor change in the watched directory or subtree causes a change notification wait operation to return.

Return Values
If the function succeeds, the return value is a handle to a find change notification object.

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

Remarks
The wait functions can monitor the specified directory or subtree by using the handle returned by the FindFirstChangeNotification function. A wait is satisfied when one of the filter conditions occurs in the monitored directory or subtree.

After the wait has been satisfied, the application can respond to this condition and continue monitoring the directory by calling the FindNextChangeNotification function and the appropriate wait function. When the handle is no longer needed, it can be closed by using the FindCloseChangeNotification function.


Symbolic link behavior—If the path points to a symbolic link, the notification handle is created for the target.

If an application has registered to receive change notifications for a directory that contains symbolic links, the application is only notified when the symbolic links have been changed, not the target files.


Transacted Operations

If there is a transaction associated with the thread at the time of the call, then that transaction is bound to the change notification handle that is returned. The change notifications that are found through enumeration on the notification handle follows the appropriate isolation rules.


Example Code
For an example, see Obtaining Directory Change_Notifications.

Requirements
Client Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server Requires Windows Server "Longhorn", Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header Declared in Winbase.h; include Windows.h.

Library Use Kernel32.lib.

DLL Requires Kernel32.dll.
Unicode Implemented as FindFirstChangeNotificationW (Unicode) and FindFirstChangeNotificationA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.


See Also
Directory Management Functions
FindCloseChangeNotification
FindNextChangeNotification
ReadDirectoryChangesW
Symbolic Links

Send comments about this topic to Microsoft

Requirements
Client Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server Requires Windows Server "Longhorn", Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header Declared in Winbase.h; include Windows.h.

Library Use Kernel32.lib.

DLL Requires Kernel32.dll.
Unicode Implemented as FindFirstChangeNotificationW (Unicode) and FindFirstChangeNotificationA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.

See Also
Directory Management Functions
FindCloseChangeNotification
FindNextChangeNotification
ReadDirectoryChangesW
Symbolic Links

Send comments about this topic to Microsoft
luolovegui 2006-10-03
  • 打赏
  • 举报
回复
呵呵.
mynamelj 2006-09-30
  • 打赏
  • 举报
回复
MSDN上的
王国凡 2006-09-30
  • 打赏
  • 举报
回复
GetFileAttributesEx 这个函数的第二个参数用 GetFileExInfoStandard 即可.
即:
TCHAR *filename = TEXT("C:\\src\\win32\\sdk\\LeafEditor\\update.txt");
WIN32_FILE_ATTRIBUTE_DATA pFileInfo;
GetFileAttributesEx(filename, GetFileExInfoStandard, &pFileInfo);
goodboyws 2006-09-30
  • 打赏
  • 举报
回复
#include <windows.h>

// GetLastWriteTime - Retrieves the last-write time and converts
// the time to a string
//
// Return value - TRUE if successful, FALSE otherwise
// hFile - Valid file handle
// lpszString - Pointer to buffer to receive string

BOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString)
{
FILETIME ftCreate, ftAccess, ftWrite;
SYSTEMTIME stUTC, stLocal;

// Retrieve the file times for the file.
if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
return FALSE;

// Convert the last-write time to local time.
FileTimeToSystemTime(&ftWrite, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

// Build a string showing the date and time.
wsprintf(lpszString, TEXT("%02d/%02d/%d %02d:%02d"),
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);

return TRUE;
}
Bennyatt 2006-09-30
  • 打赏
  • 举报
回复
这个 可以 GET几个TIME呀??
文件有 访问时间 修改时间 创建时间.....
楼主 加油哦
mynamelj 2006-09-30
  • 打赏
  • 举报
回复
GetFileTime
用这个函数吧
DentistryDoctor 2006-09-30
  • 打赏
  • 举报
回复
是调用不成功吗?
大志哥123 2006-09-30
  • 打赏
  • 举报
回复
顶下

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧