最近在windows mobile 平台上开发,因为国内物流行业多数现在使用的PDA还是这样老掉牙的wm操作系统,没办法。别说什么已经淘汰的塞班、这么火的安卓、IOS。谁还用windows mobile开发。 用的是VS2008 基于.net compact framework,没办法,我只能说是阉割版的.net框架,好多东西都没有了,比如说groupbox控件,比如说FTP这东西,比如说listview控件的自动调整列宽的属性、等等,都被阉割掉了。 如果说在PC平台上,做个FTP那不是很轻松的事情,大家都会使用FtpWebRequest这个类,但是用C#在windows mobile上开发FTP就没那么简单了。但是能不能实现呢?我告诉你,100%可以实现,放心去做吧。废话少说,开始吧。必须使用wininet.dll库,这是毫无疑问的。
1、因为下面使用相关API的需要,所以添加以下结构体:// service types for InternetOpen() public enum ACCESS_TYPE_FOR_INTERNETOPEN { INTERNET_OPEN_TYPE_PRECONFIG=0, // use registry configuration INTERNET_OPEN_TYPE_DIRECT=1, // direct to net INTERNET_OPEN_TYPE_PROXY=3, // via named proxy INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY=4 // prevent using java/script/INS }; // service types for InternetConnect() public enum ACCESS_TYPE_FOR_INTERNETCONNECT { INTERNET_SERVICE_FTP=1, INTERNET_SERVICE_GOPHER=2, INTERNET_SERVICE_HTTP=3 }; // protocol-specific flags. public enum PROTOCOL_SPECIFIC_FLAG { INTERNET_FLAG_PASSIVE=0x08000000 // used for FTP connections }; // These are the generic rights. public enum ACCESS_TYPE_FOR_FILE: uint { GENERIC_READ=0x80000000, GENERIC_WRITE=0x40000000, GENERIC_EXECUTE=0x20000000, GENERIC_ALL=0x10000000 }; // FTP manifests public enum ACCESS_TYPE_FOR_TRANSFER { FTP_TRANSFER_TYPE_UNKNOWN=0x00000000, FTP_TRANSFER_TYPE_ASCII=0x00000001, FTP_TRANSFER_TYPE_BINARY=0x00000002 }; public enum FILE_ATTRIBUTE:uint { FILE_ATTRIBUTE_READONLY=0x00000001, FILE_ATTRIBUTE_HIDDEN= 0x00000002, FILE_ATTRIBUTE_SYSTEM=0x00000004, FILE_ATTRIBUTE_DIRECTORY=0x00000010, FILE_ATTRIBUTE_ARCHIVE=0x00000020, FILE_ATTRIBUTE_INROM=0x00000040, FILE_ATTRIBUTE_NORMAL=0x00000080, FILE_ATTRIBUTE_TEMPORARY=0x00000100, FILE_ATTRIBUTE_SPARSE_FILE=0x00000200, MODULE_ATTR_NOT_TRUSTED=0x00000200, FILE_ATTRIBUTE_REPARSE_POINT=0x00000400, MODULE_ATTR_NODEBUG=0x00000400, FILE_ATTRIBUTE_COMPRESSED0x00000800, FILE_ATTRIBUTE_OFFLINE0x00001000, FILE_ATTRIBUTE_ROMSTATICREF=0x00001000, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED=0x00002000, FILE_ATTRIBUTE_ROMMODULE=0x00002000, FILE_ATTRIBUTE_ENCRYPTED=0x00004000 }; 2、加入以下的接口引用 //wininet for FTP [DllImport("wininet.dll",EntryPoint="InternetOpen")] extern static IntPtr InternetOpen(string lpszAgent, UInt32 dwAccessType, string lpszProxyName, string lpszProxyBypass, UInt32 dwFlags); [DllImport("wininet.dll", EntryPoint = "InternetConnect")] extern static IntPtr InternetConnect(IntPtr hInternet, string lpszServerName, UInt16 nServerPort, string lpszUserName, string lpszPassword, UInt32 dwService, UInt32 dwFlags, UInt32 dwContext); [DllImport("wininet.dll", EntryPoint = "InternetReadFile")] extern static bool InternetReadFile(IntPtr hFile,byte[] lpBuffer,int dwNumberOfBytesToRead,out int lpdwNumberOfBytesRead); [DllImport("wininet.dll", EntryPoint = "InternetWriteFile")] extern static bool InternetWriteFile(IntPtr hFile,IntPtr lpBuffer,UInt32 dwNumberOfBytesToWrite,IntPtr lpdwNumberOfBytesWritten); [DllImport("wininet.dll", EntryPoint = "InternetCloseHandle")] extern static bool InternetCloseHandle(IntPtr hInternet); [DllImport("wininet.dll", EntryPoint = "FtpOpenFile")] extern static IntPtr FtpOpenFile(IntPtr hConnect, string lpszFileName, ACCESS_TYPE_FOR_FILE dwAccess, ACCESS_TYPE_FOR_TRANSFER dwFlags, UInt32 dwContext); [DllImport("wininet.dll", EntryPoint = "FtpGetFileSize")] extern static UInt32 FtpGetFileSize(IntPtr hFile,IntPtr lpdwFileSizeHigh); [DllImport("wininet.dll", EntryPoint = "FtpGetFile")] extern static bool FtpGetFile(IntPtr hConnect, string lpszRemoteFile, string lpszNewFile, bool fFailIfExists, UInt32 dwFlagsAndAttributes, UInt32 dwFlags, UInt32 dwContext); [DllImport("wininet.dll", EntryPoint = "FtpPutFile")] extern static bool FtpPutFile(IntPtr hConnect,string lpszLocalFile,string lpszNewRemoteFile,UInt32 dwFlags,UInt32 dwContext); [DllImport("wininet.dll", EntryPoint = "FtpGetCurrentDirectory")] extern static bool FtpGetCurrentDirectory(IntPtr hConnect,string lpszCurrentDirectory,IntPtr lpdwCurrentDirectory); [DllImport("wininet.dll", EntryPoint = "FtpSetCurrentDirectory")] extern static bool FtpSetCurrentDirectory(IntPtr hConnect,string lpszDirectory); [DllImport("wininet.dll", EntryPoint = "FtpCreateDirectory")] extern static bool FtpCreateDirectory(IntPtr hConnect,string lpszDirectory); 3、具体怎么使用这些接口,大家可以去看MSDN了,我不在这里赘述了。