你的代码:System.DateTime d1=System.DateTime.FromFileTime(fd.ftLastWriteTime.dwHighDateTime);
MessageBox.Show(d1.ToString());
System.DateTime d2=System.DateTime.FromFileTime(fd.ftLastWriteTime.dwLowDateTime);应该是不对的,你需要将dwLowDateTime和dwHighDateTime作为一个Long类型数据来处理。应该这样:
FILETIME ft = new FILETIME(); /////////////////////////////////////////////////////////////////
//from System.DateTime to System.Runtime.InteropServices.FILETIME
/////////////////////////////////////////////////////////////////
long hFT1 = DateTime.Now.ToFileTimeUtc();
ft.dwLowDateTime = (int) (hFT1 & 0xFFFFFFFF);
ft.dwHighDateTime = (int) (hFT1 >> 32); /////////////////////////////////////////////////////////////////
//from System.Runtime.InteropServices.FILETIME to System.DateTime
/////////////////////////////////////////////////////////////////
long hFT2 = (((long) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
DateTime dte = DateTime.FromFileTimeUtc(hFT2);
MessageBox.Show(d1.ToString());
System.DateTime d2=System.DateTime.FromFileTime(fd.ftLastWriteTime.dwLowDateTime);应该是不对的,你需要将dwLowDateTime和dwHighDateTime作为一个Long类型数据来处理。应该这样:
FILETIME ft = new FILETIME(); /////////////////////////////////////////////////////////////////
//from System.DateTime to System.Runtime.InteropServices.FILETIME
/////////////////////////////////////////////////////////////////
long hFT1 = DateTime.Now.ToFileTimeUtc();
ft.dwLowDateTime = (int) (hFT1 & 0xFFFFFFFF);
ft.dwHighDateTime = (int) (hFT1 >> 32); /////////////////////////////////////////////////////////////////
//from System.Runtime.InteropServices.FILETIME to System.DateTime
/////////////////////////////////////////////////////////////////
long hFT2 = (((long) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
DateTime dte = DateTime.FromFileTimeUtc(hFT2);
搞错了WIN32_FIND_DATA 的定义变量类型,把下面UInt32改动为int类型不对。
应为:
[StructLayout(LayoutKind.Sequential)]
public struct WIN32_FIND_DATA
{
public UInt32 dwFileAttributes;
public FILETIME ftCreationTme;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public UInt32 nFileSizeHigh;
public UInt32 nFileSizeLow;
public UInt32 dwReserved0;
public UInt32 dwReserved1;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=256)] public string cFileName;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=14)] public string cAlternate;
}
结果如下:
private void button1_Click(object sender, System.EventArgs e)
{
int hopen,hconet,hftpfind ;
string str_hk_serverip="100.12.4.20";
string str_user="test";
string str_passwd="test";hopen=InternetOpen("c# ftp",INTERNET_OPEN_TYPE_DIRECT,"","",0);
hconet=InternetConnect(hopen,str_hk_serverip,21,str_user,str_passwd,INTERNET_SERVICE_FTP,0,0);
string find_file="/usr/home/test.txt";WIN32_FIND_DATA fd = new WIN32_FIND_DATA();
hftpfind =FtpFindFirstFile(hconet,find_file,ref fd,2,0);
long hFT2 = (((long) fd.ftLastWriteTime.dwHighDateTime) << 32) + (long)fd.ftLastWriteTime.dwLowDateTime;
//MessageBox.Show(hFT2.ToString());
DateTime dte = DateTime.FromFileTimeUtc(hFT2);
MessageBox.Show(dte.ToString());
InternetCloseHandle(hconet);
InternetCloseHandle(hopen);
}