【转】Wince FTP WinAPI 大放送
本帖最后由 91program 于 2013-05-16 19:05:23 编辑 http://bbs.csdn.net/topics/390460167
看到这么好的帖子,竟然是 WinCE 下 FTP 的实现,但却发在 .NET技术 > C# 区,相信部分 CE 的爱好者不一定能看到,所以转到 CE 的坛子。
多谢 zhouzhangkui 开源出来!
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;<br>
namespace SimpleFtp
{
internal class WinAPI
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class WIN32_FIND_DATA
{
public UInt32 dwFileAttributes = 0;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public UInt32 nFileSizeHigh = 0;
public UInt32 nFileSizeLow = 0;
public UInt32 dwReserved0 = 0;
public UInt32 dwReserved1 = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string cFileName = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName = null;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FILETIME
{
public int dwLowDateTime = 0;
public int dwHighDateTime = 0;
};<br>
public const int INTERNET_FLAG_PASSIVE = 0x8000000; //被动模式
public const int INTERNET_FLAG_PORT = 0x0; //主动模式<br>
public const uint INTERNET_FLAG_RELOAD = 0x80000000; //
public const uint INTERNET_FLAG_KEEP_CONNECTION = 0x400000; //
public const uint INTERNET_FLAG_MULTIPART = 0x200000; //<br>
public const int INTERNET_OPEN_TYPE_PRECONFIG = 0;
public const int INTERNET_OPEN_TYPE_DIRECT = 1;
public const int INTERNET_OPEN_TYPE_PROXY = 3;<br>
public const int INTERNET_SERVICE_FTP = 1;
public const int INTERNET_SERVICE_GOPHER = 2;
public const int INTERNET_SERVICE_HTTP = 3;<br>
public const uint FTP_TRANSFER_TYPE_ASCII = 0x1;
public const uint FTP_TRANSFER_TYPE_BINARY = 0x2;<br>
public const int FILE_ATTRIBUTE_READONLY = 0x1;
public const int FILE_ATTRIBUTE_HIDDEN = 0x2;
public const int FILE_ATTRIBUTE_SYSTEM = 0x4;
public const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
public const int FILE_ATTRIBUTE_ARCHIVE = 0x20;
public const int FILE_ATTRIBUTE_NORMAL = 0x80;
public const int FILE_ATTRIBUTE_TEMPORARY = 0x100;
public const int FILE_ATTRIBUTE_COMPRESSED = 0x800;
// 连接和初始化
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern void SetLastError(int dwErrCode);<br>
// 连接和初始化
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr InternetOpen(string strAppName, int nAccessType, string strProxy, string strProxyBypass, int nFlags);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr InternetConnect(IntPtr hInet, string strServer, int nPort, string strUser, string strPassword
, int nService, int nFlags, int nContext);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetCloseHandle(IntPtr hSession);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetGetConnectedState(ref int ulFlags, int ulReserved);<br>
// Ftp文件操作命令
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr FtpFindFirstFile(IntPtr hSession, string strPath, [In, Out] WIN32_FIND_DATA dirData, int nFlags, int nContext);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetFindNextFile(IntPtr hFind, [In, Out] WIN32_FIND_DATA dirData);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpGetFile(IntPtr hFtpSession, string lpszRemoteFile, string lpszNewFile
, bool fFailIfExists, int dwFlagsAndAttributes, uint dwFlags, int dwContext);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpPutFile(IntPtr hFtpSession, string lpszLocalFile, string lpszRemoteFile
, uint dwFlags, int dwContext);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpDeleteFile(IntPtr hFtpSession, string lpszFileName);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpRenameFile(IntPtr hFtpSession, string lpszExisting, string lpszNew);<br>
// Ftp目录操作命令
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpGetCurrentDirectory(IntPtr hFtpSession, [In, Out] string lpszCurrentDirectory, ref int lpdwCurrentDirectory);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpSetCurrentDirectory(IntPtr hFtpSession, string lpszCurrentDirectory);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpCreateDirectory(IntPtr hFtpSession, string lpszDirectory);<br>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpRemoveDirectory(IntPtr hFtpSession, string lpszDirectory);<br>
}
}
[解决办法]
挺好,收藏着,楼主有心了
[解决办法]
http://blog.csdn.net/oushengfen/article/details/8863009
我前一阵子,就共享出来了,楼主上述代码在WINCE下是不能正常使用的。我的是在WINCE下使用过的。
[解决办法]
虽然没使用过,但是代码写的不错!
[解决办法]
前了阵子,我也是苦于没有这样的DEMO,在各方的努力下,实现了。然后分享出来。
希望有更多人分享更多更好的东西,供大家学习,共同进步。。。