当前位置: 代码迷 >> PB >> 关于怎么获取文件修改日期的方法
  详细解决方案

关于怎么获取文件修改日期的方法

热度:74   发布时间:2016-04-29 08:33:07.0
关于如何获取文件修改日期的方法
如题,我在网上找到的一种方法:

string filename= "C:\powerbuilder\PbApi\WinApi.txt " 
ulong hfile 
long rtn 
WIN32_FIND_DATA lpFindFileData 
SYSTEMTIME lpSystemTime 
filetime lpcreate 
filetime lpaccess 
filetime lpwrite 
FILETIME lpFileTime 
FindFirstFile(filename,lpFindFileData) 
lpFileTime = lpFindFileData.creationtime 

rtn = FileTimeToSystemTime(lpFileTime,lpSystemTime) 
Messagebox( "creationtime ", string(lpSystemTime.wyear)+ "年 "+string(lpSystemTime.wmonth)+ "月 "+string(lpSystemTime.wday)+ "日 ") 


相关结构: 
$PBExportHeader$win32_find_data.srs 
global type WIN32_FIND_DATA from structure 
unsignedlong fileattributes 
filetime creationtime 
filetime lastaccesstime 
filetime lastwritetime 
unsignedlong filesizehigh 
unsignedlong filesizelow 
unsignedlong reserved0 
unsignedlong reserved1 
character filename[260] 
character alternatefilename[14] 
end type 

$PBExportHeader$filetime.srs 
global type FILETIME from structure 
ulong dwLowDateTime 
ulong dwHighDateTime 
end type 

$PBExportHeader$systemtime.srs 
global type SYSTEMTIME from structure 
uint wYear 
uint wMonth 
uint wDayOfWeek 
uint wDay 
uint wHour 
uint wMinute 
uint wSecond 
uint wMilliseconds 
end type 

函数定义 
FUNCTION ulong FileTimeToSystemTime(ref FILETIME lpFileTime,ref SYSTEMTIME lpSystemTime) LIBRARY "kernel32.dll " 
FUNCTION ulong FindFirstFile(ref string lpFileName,ref WIN32_FIND_DATA lpFindFileData) LIBRARY "kernel32.dll " ALIAS FOR "FindFirstFileA "

但是,运行时总是提示下面几行有问题:
FILETIME lpFileTime 
FindFirstFile(filename,lpFindFileData) 
lpFileTime = lpFindFileData.creationtime 

rtn = FileTimeToSystemTime(lpFileTime,lpSystemTime) 

问题出在什么地方,是否还有其它好的办法,请各位多多指教


------解决方案--------------------
建立一个userobject,命名为n_cst_liveupdate
然后对该对象edit source ,将下面那些内容复制替换掉里面的内容,然后保存
n_cst_liveupdate是一个取(和设置)本地文件最后修改日期的自定义对象

C/C++ code
forwardglobal type n_cst_liveupdate from nonvisualobjectend typetype os_filedatetime from structure within n_cst_liveupdateend typetype os_fileopeninfo from structure within n_cst_liveupdateend typetype os_finddata from structure within n_cst_liveupdateend typetype os_securityattributes from structure within n_cst_liveupdateend typetype os_systemtime from structure within n_cst_liveupdateend typeend forwardtype os_filedatetime from structure    unsignedlong        ul_lowdatetime    unsignedlong        ul_highdatetimeend typetype os_fileopeninfo from structure    character        c_length    character        c_fixed_disk    unsignedinteger        ui_dos_error    unsignedinteger        ui_na1    unsignedinteger        ui_na2    character        c_pathname[128]end typetype os_finddata from structure    unsignedlong        ul_fileattributes    os_filedatetime        str_creationtime    os_filedatetime        str_lastaccesstime    os_filedatetime        str_lastwritetime    unsignedlong        ul_filesizehigh    unsignedlong        ul_filesizelow    unsignedlong        ul_reserved0    unsignedlong        ul_reserved1    character        ch_filename[260]    character        ch_alternatefilename[14]end typetype os_securityattributes from structure    unsignedlong        ul_length    string        ch_description    boolean        b_inheritend typetype os_systemtime from structure    unsignedinteger        ui_wyear    unsignedinteger        ui_wmonth    unsignedinteger        ui_wdayofweek    unsignedinteger        ui_wday    unsignedinteger        ui_whour    unsignedinteger        ui_wminute    unsignedinteger        ui_wsecond    unsignedinteger        ui_wmillisecondsend typeglobal type n_cst_liveupdate from nonvisualobject autoinstantiateend typetype prototypes//获得应用程序名用//Function uint GetModuleFileNameA(ulong hModule,ref string lpFilename,ulong nSize) Library "kernel32.dll" //获取应用程序运行目录//文件操作Function long FindFirstFileA (ref string filename, ref os_finddata findfiledata) library "kernel32.dll"Function boolean FindNextFileA (long handle, ref os_finddata findfiledata) library "kernel32.dll"Function boolean FindClose (long handle) library "kernel32.dll"Function long    OpenFile (ref string filename, ref os_fileopeninfo of_struct, ulong action) LIBRARY "kernel32.dll"Function boolean CloseHandle (long file_hand) LIBRARY "kernel32.dll"Function boolean GetFileTime(long hFile, ref os_filedatetime  lpCreationTime, ref os_filedatetime  lpLastAccessTime, ref os_filedatetime  lpLastWriteTime  )  library "kernel32.dll"Function boolean FileTimeToSystemTime(ref os_filedatetime lpFileTime, ref os_systemtime lpSystemTime) library "kernel32.dll"Function boolean FileTimeToLocalFileTime(ref os_filedatetime lpFileTime, ref os_filedatetime lpLocalFileTime) library "kernel32.dll"Function boolean SetFileTime(long hFile, os_filedatetime  lpCreationTime, os_filedatetime  lpLastAccessTime, os_filedatetime  lpLastWriteTime  )  library "kernel32.dll"Function boolean SystemTimeToFileTime(os_systemtime lpSystemTime, ref os_filedatetime lpFileTime) library "kernel32.dll"Function boolean LocalFileTimeToFileTime(ref os_filedatetime lpLocalFileTime, ref os_filedatetime lpFileTime) library "kernel32.dll"end prototypestype variablesend variablesforward prototypespublic function integer of_convertfiledatetimetopb (os_filedatetime astr_filetime, ref date ad_filedate, ref time at_filetime)public function integer of_convertpbdatetimetofile (date ad_filedate, time at_filetime, ref os_filedatetime astr_filetime)public function integer of_getcreatedatetime (string as_filename, ref date ad_date, ref time at_time)public function integer of_getlastwritedatetime (string as_filename, ref date ad_date, ref time at_time)public function integer of_setlastwritedatetime (string as_filename, date ad_date, time at_time)public function integer of_convertfiledatetimetopb (os_filedatetime astr_filetime, ref date ad_filedate, ref time at_filetime)public function integer of_convertpbdatetimetofile (date ad_filedate, time at_filetime, ref os_filedatetime astr_filetime)public function integer of_getcreatedatetime (string as_filename, ref date ad_date, ref time at_time)public function integer of_getlastwritedatetime (string as_filename, ref date ad_date, ref time at_time)public function integer of_setlastwritedatetime (string as_filename, date ad_date, time at_time)end prototypespublic function integer of_convertfiledatetimetopb (os_filedatetime astr_filetime, ref date ad_filedate, ref time at_filetime);string                ls_Date, ls_Timeos_filedatetime    lstr_LocalTimeos_systemtime    lstr_SystemTimeIf Not FileTimeToLocalFileTime(astr_FileTime, lstr_LocalTime) Then Return -1If Not FileTimeToSystemTime(lstr_LocalTime, lstr_SystemTime) Then Return -1ls_Date = String(lstr_SystemTime.ui_wyear) + "/" + &             String(lstr_SystemTime.ui_WMonth) + "/" + &             String(lstr_SystemTime.ui_WDay)ad_FileDate = Date(ls_Date)ls_Time = String(lstr_SystemTime.ui_wHour) + ":" + &             String(lstr_SystemTime.ui_wMinute) + ":" + &             String(lstr_SystemTime.ui_wSecond) + ":" + &             String(lstr_SystemTime.ui_wMilliseconds)at_FileTime = Time(ls_Time)Return 1end functionpublic function integer of_convertpbdatetimetofile (date ad_filedate, time at_filetime, ref os_filedatetime astr_filetime);////////////////////////////////////////////////////////////////////////////////    Protected Function:  of_ConvertPBDatetimeToFile//    Arguments:        ad_FileDate            The file date in PowerBuilder Date format.//                        at_FileTime            The file time in PowerBuilder Time format.//                        astr_FileTime        The os_filedatetime structure to contain the system date/time for the file, passed by reference.//    Returns:            Integer//                        1 if successful, -1 if an error occurrs.//    Description:    Convert PowerBuilder Date and Time to the sytem file type.////////////////////////////////////////////////////////////////////////////////    Rev. History:    Version//                        6.0   Initial version//                        6.0.01    Fix millisecond overflow.  Change size of string to 3 digits from 6 ////////////////////////////////////////////////////////////////////////////////    Copyright ?1996-1999 Sybase, Inc. and its subsidiaries.  All rights reserved.  Any distribution of the // PowerBuilder Foundation Classes (PFC) source code by other than Sybase, Inc. and its subsidiaries is prohibited.//////////////////////////////////////////////////////////////////////////////string                ls_Date, ls_Timeos_filedatetime    lstr_LocalTimeos_systemtime    lstr_SystemTimels_Date = String(ad_FileDate, "yyyy-mm-dd")lstr_SystemTime.ui_wyear = Long(Left(ls_Date, 4))lstr_SystemTime.ui_WMonth = Long(Mid(ls_Date, 6, 2))lstr_SystemTime.ui_WDay = Long(Right(ls_Date, 2))ls_Time = String(at_FileTime, "hh:mm:ss:fff")lstr_SystemTime.ui_wHour = Long(Left(ls_Time, 2))lstr_SystemTime.ui_wMinute = Long(Mid(ls_Time, 4, 2))lstr_SystemTime.ui_wSecond = Long(Mid(ls_Time, 7, 2))lstr_SystemTime.ui_wMilliseconds = Long(Right(ls_Time, 3))If Not SystemTimeToFileTime(lstr_SystemTime, lstr_LocalTime) Then Return -1If Not LocalFileTimeToFileTime(lstr_LocalTime, astr_FileTime) Then Return -1Return 1end functionpublic function integer of_getcreatedatetime (string as_filename, ref date ad_date, ref time at_time);//得到文件创建的时间long ll_handleos_finddata    lstr_FindData// Get the file informationll_handle = FindFirstFileA(as_FileName, lstr_FindData)If ll_handle <= 0 Then Return -1FindClose(ll_handle)// Convert the date and timeReturn of_ConvertFileDatetimeToPB(lstr_FindData.str_CreationTime, ad_Date, at_Time)end functionpublic function integer of_getlastwritedatetime (string as_filename, ref date ad_date, ref time at_time);//得到文件最后修改的时间long    ll_handleos_finddata    lstr_FindData// Get the file informationll_handle = FindFirstFileA(as_FileName, lstr_FindData)If ll_handle <= 0 Then Return -1FindClose(ll_handle)// Convert the date and timeReturn of_ConvertFileDatetimeToPB(lstr_FindData.str_LastWriteTime, ad_Date, at_Time)end functionpublic function integer of_setlastwritedatetime (string as_filename, date ad_date, time at_time);boolean lb_Retlong ll_handleos_filedatetime lstr_FileTime, lstr_Emptyos_finddata lstr_FindDataos_fileopeninfo lstr_FileInfo// Get the file information.// This is required to keep the Last Access date from changing.// It will be changed by the OpenFile function.ll_handle = FindFirstFileA(as_FileName, lstr_FindData)If ll_handle <= 0 Then Return -1FindClose(ll_handle)// Convert the date and timeIf of_ConvertPBDatetimeToFile(ad_Date, at_Time, lstr_FileTime) < 0 Then Return -1// Set the file structure informationlstr_FileInfo.c_fixed_disk = "~000"lstr_FileInfo.c_pathname = as_FileNamelstr_FileInfo.c_length = "~142"// Open the filell_handle = OpenFile ( as_filename, lstr_FileInfo, 2 ) If ll_handle < 1 Then Return -1 lb_Ret = SetFileTime(ll_handle, lstr_Empty, lstr_FindData.str_LastAccessTime, lstr_FileTime)CloseHandle(ll_handle)If lb_Ret Then    Return 1Else    Return -1End Ifend functionon n_cst_liveupdate.createcall super::createTriggerEvent( this, "constructor" )end onon n_cst_liveupdate.destroyTriggerEvent( this, "destructor" )call super::destroyend on
  相关解决方案