to see such a good post, turned out to be under WinCE FTP implementation , but made in the . NET technologies > C # area, I believe some CE enthusiasts may not be able to see , so go to the CE jar .
Thank zhouzhangkui source out !
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>
}
}
------ Solution ------------------------------------ --------
good, collection of , the landlord determined the
------ Solution - -------------------------------------------
http:// blog.csdn.net/oushengfen/article/details/8863009
me a while ago , they shared out , the landlord is the above code in WINCE not normally use . I was in WINCE used .
------ Solution ------------------------------------ --------
Although not used, but the code was pretty good !
------ Solution ---------------------------------------- ----
before the time around , I also suffer from no such DEMO, the efforts of the parties to achieve . Then share it .
hope more people share more and better things for everyone to learn and progress together . . .
------ For reference only ---------------------------------- -----
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.ComponentModel;<br>
namespace SimpleFtp
{
internal class FtpAccess
{
private string m_szIP = null;
private int m_nPort = -1;
private string m_szUserName = null;
private string m_szPassword = null;<br>
private IntPtr m_hInternet = IntPtr.Zero;
private IntPtr m_hConnect = IntPtr.Zero;<br>
/// <summary>
/// 获取或设置FTP的访问IP地址
/// </summary>
public string IP
{
get
{
return this.m_szIP;
}
set
{
this.m_szIP = value;
}
}
/// <summary>
/// 获取或设置FTP的访问端口号
/// </summary>
public int Port
{
get
{
return this.m_nPort;
}
set
{
this.m_nPort = value;
}
}
/// <summary>
/// 获取或设置FTP的访问用户名称
/// </summary>
public string UserName
{
get
{
return this.m_szUserName;
}
set
{
this.m_szUserName = value;
}
}
/// <summary>
/// 获取或设置FTP的访问用户密码
/// </summary>
public string Password
{
get
{
return this.m_szPassword;
}
set
{
this.m_szPassword = value;
}
}<br>
/// <summary>
/// 连接到FTP指定的路径
/// </summary>
/// <param name="path">路径</param>
public bool Connect()
{
this.InitWinAPI();
if (this.m_hConnect != IntPtr.Zero)
{
if (WinAPI.FtpSetCurrentDirectory(this.m_hConnect, "/"))
return true;
}
this.CloseConnection();<br>
if (this.m_szIP == null || this.m_szUserName == null || this.m_szPassword == null)
{
//CommonDef.SetLastError("FTP站点连接失败!FTP访问参数未设置");
return false;
}<br>
this.m_hInternet = WinAPI.InternetOpen("ftp", WinAPI.INTERNET_OPEN_TYPE_PRECONFIG, null, null, 0);
if (this.m_hInternet == null || this.m_hInternet == IntPtr.Zero)
{
//CommonDef.SetLastError("FTP站点连接失败!" + this.GetWin32Error());
return false;
}<br>
this.m_hConnect = WinAPI.InternetConnect(this.m_hInternet, this.IP, this.Port, this.UserName, this.Password
, WinAPI.INTERNET_SERVICE_FTP, WinAPI.INTERNET_FLAG_PASSIVE, 0);
if (this.m_hConnect == null || this.m_hConnect == IntPtr.Zero)
{
//CommonDef.SetLastError("FTP站点连接失败!" + this.GetWin32Error());
return false;
}<br>
if (!WinAPI.FtpSetCurrentDirectory(this.m_hConnect, "/"))
{
//CommonDef.SetLastError("FTP站点连接失败!" + this.GetWin32Error());
return false;
}
return true;
}
/// <summary>
/// 关闭FTP连接
/// </summary>
public void CloseConnection()
{
if (this.m_hConnect != IntPtr.Zero)
{
WinAPI.InternetCloseHandle(this.m_hConnect);
this.m_hConnect = IntPtr.Zero;
}
if (this.m_hInternet != IntPtr.Zero)
{
WinAPI.InternetCloseHandle(this.m_hInternet);
this.m_hInternet = IntPtr.Zero;
}
}
/// <summary>
/// 判断文件或文件夹是否存在
/// </summary>
/// <param name="path">文件或文件夹路径</param>
/// <param name="bIsFolder">资源是否是文件夹</param>
/// <returns>true:资源存在;false:资源不存在</returns>
public bool ResExists(string szResPath, bool bIsFolder)
{
szResPath = szResPath.Trim();
if (szResPath == "/" || szResPath == "")
return true;<br>
if (!this.Connect())
return false;<br>
this.InitWinAPI();
//if (bIsFolder)
//{
if (!WinAPI.FtpSetCurrentDirectory(this.m_hConnect, szResPath))
{
return false;
}
if (!WinAPI.FtpSetCurrentDirectory(this.m_hConnect, "/"))
{
return false;
}
return true;
//}
//else
//{
// WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
// IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szResPath, stFileInfo, 0, 0);
// if (hFile == null || hFile == IntPtr.Zero)
// {
// return false;
// }
// WinAPI.InternetCloseHandle(hFile);
// return true;
//}
}<br>
/// <summary>
/// 删除一个本地文件
/// </summary>
/// <param name="szFileName">本地文件</param>
/// <param name="bRaiseException">是否抛出异常</param>
/// <returns>bool</returns>
private bool DeleteFile(string szFileName, bool bRaiseException)
{
if (!File.Exists(szFileName))
return true;
try
{
File.Delete(szFileName);
return true;
}
catch (Exception ex)
{
if (bRaiseException)
throw ex;
return false;
}
}<br>
private bool RenameFile(string szFileName, string szNewName, bool bRaiseException)
{
if (!File.Exists(szFileName))
return true;
try
{
File.Move(szFileName, szNewName);
return true;
}
catch (Exception ex)
{
if (bRaiseException)
throw ex;
return false;
}
}<br>
/// <summary>
/// 上传本地文件到服务器
/// </summary>
/// <param name="szLocalFile">本地源文件全路径</param>
/// <param name="szRemoteFile">目的服务器文件全路径</param>
/// <returns>true:上传成功;false:上传失败</returns>
public bool Upload(string szLocalFile, string szRemoteFile)
{
FileInfo fileInfo = new FileInfo(szLocalFile);
if (fileInfo == null || !fileInfo.Exists)
return false;<br>
if (!File.Exists(szLocalFile))
{
//CommonDef.SetLastError(string.Format("文件{0}上传失败!本地文件不存在!", szLocalFile));
return false;
}<br>
//if (this.ResExists(szRemoteFile, false))
//{
// string szErrorInfo = string.Format("文件{0}上传到{1}失败!远程文件已经存在!"
// , szLocalFile, szRemoteFile);
//CommonDef.SetLastError(szErrorInfo);
// return false;
//}<br>
this.InitWinAPI();
if (!WinAPI.FtpPutFile(this.m_hConnect, szLocalFile, szRemoteFile, WinAPI.FTP_TRANSFER_TYPE_BINARY | WinAPI.INTERNET_FLAG_RELOAD, 0))
{
string szErrorInfo = string.Format("文件{0}上传到文件{1}失败!", szLocalFile, szRemoteFile);
//CommonDef.SetLastError(szErrorInfo + this.GetWin32Error());
return false;
}
return true;
}
/// <summary>
/// 下载指定的FTP路径上的文件
/// </summary>
/// <param name="szRemoteFile">远程文件</param>
/// <param name="szLocalFile">保存本地文件名</param>
/// <returns>true:下载成功;false:下载失败</returns>
public bool Download(string szRemoteFile, string szLocalFile)
{
try
{
this.RenameFile(szLocalFile, szLocalFile + ".tmp", true);
}
catch (Exception ex)
{
string szErrorInfo = string.Format("文件{0}下载到文件{1}失败!无法重命名本地已存在文件"
, szRemoteFile, szLocalFile);
//CommonDef.SetLastError(szErrorInfo + ex.Message);
return false;
}<br>
//if (!this.ResExists(szRemoteFile, false))
//{
//CommonDef.SetLastError(string.Format("文件{0}下载失败!远程文件不存在!", szRemoteFile));
// return false;
//}<br>
this.InitWinAPI();
//INTERNET_FLAG_RELOAD..清空缓存...
if (!WinAPI.FtpGetFile(this.m_hConnect, szRemoteFile, szLocalFile, false, 0, WinAPI.FTP_TRANSFER_TYPE_BINARY | WinAPI.INTERNET_FLAG_RELOAD, 0))
{
//更改文件名,否则FtpGetFile函数会自动删除本地的文件...
this.RenameFile(szLocalFile + ".tmp", szLocalFile, false);
string szError = this.GetWin32Error();
string szErrorInfo = string.Format("文件{0}下载到文件{1}失败!", szRemoteFile, szLocalFile);
//CommonDef.SetLastError(szErrorInfo + this.GetWin32Error());
return false;
}
if (!File.Exists(szLocalFile))
{
//更改文件名,否则FtpGetFile函数会自动删除本地的文件...
this.RenameFile(szLocalFile + ".tmp", szLocalFile, false);
string szErrorInfo = string.Format("文件{0}下载到文件{1}失败!", szRemoteFile, szLocalFile);
//CommonDef.SetLastError(szErrorInfo);
return false;
}
//删除本地的临时文件...
this.DeleteFile(szLocalFile + ".tmp", false);
return true;
}
------ For reference only --------------------------- ------------
/// <summary>
/// 创建指定路径上所有缺失的目录
/// </summary>
/// <param name="dirPath">目录</param>
/// <returns>bool</returns>
public bool CreateDirectory(string szDirPath)
{
if (this.ResExists(szDirPath, true))
return true;<br>
string[] arrDirName = szDirPath.Split(new char[] { '/' });
szDirPath = string.Empty;
for (int index = 0; index < arrDirName.Length; index++)
{
string szDirName = arrDirName[index];
if (szDirName == null || szDirName.Trim() == string.Empty)
continue;
szDirPath += "/" + szDirName;
if (this.ResExists(szDirPath, true))
continue;
this.InitWinAPI();<br>
if (!WinAPI.FtpCreateDirectory(this.m_hConnect, szDirPath))
{
string szErrorInfo = string.Format("目录{0}创建失败!", szDirPath);
//CommonDef.SetLastError(szErrorInfo + this.GetWin32Error());
return false;
}
}
return true;
}
/// <summary>
/// 获取指定目录的文件列表
/// </summary>
/// <param name="szDirPath">目录路径</param>
/// <param name="bFolder">是否为文件夹属性</param>
/// <param name="lstFilePath">返回的文件列表</param>
/// <returns>bool</returns>
public bool GetFileList(string szDirPath, bool bFolder, ref List<string> lstFilePath)
{
if (!this.ResExists(szDirPath, true))
{
string szErrorInfo = string.Format("列表目录{0}下的文件失败!远程目录不存在", szDirPath);
//CommonDef.SetLastError(szErrorInfo + this.GetWin32Error());
return false;
}
WinAPI.WIN32_FIND_DATA stFileInfo = new WinAPI.WIN32_FIND_DATA();
IntPtr hFile = WinAPI.FtpFindFirstFile(this.m_hConnect, szDirPath, stFileInfo, 0, 0);
if (hFile == IntPtr.Zero)
return false;<br>
if (lstFilePath == null)
lstFilePath = new List<string>();<br>
// 遍历父目录查找指定的子目录是否存在
bool bSuccess = true;
while (bSuccess)
{
if (bFolder)
{
if (this.IsDirectory(stFileInfo.dwFileAttributes))
{
string szChildPath = string.Format("{0}/{1}", szDirPath, stFileInfo.cFileName);
lstFilePath.Add(szChildPath);
}
}
else
{
if (!this.IsDirectory(stFileInfo.dwFileAttributes))
{
string szChildPath = string.Format("{0}/{1}", szDirPath, stFileInfo.cFileName);
lstFilePath.Add(szChildPath);
}
}
bSuccess = WinAPI.InternetFindNextFile(hFile, stFileInfo);
}
WinAPI.InternetCloseHandle(hFile);
return true;
}
/// <summary>
/// 指定的文件属性是否是文件夹属性
/// </summary>
/// <param name="dwFileAttributes">文件属性</param>
/// <returns>bool</returns>
private bool IsDirectory(UInt32 dwFileAttributes)
{
if ((dwFileAttributes & WinAPI.FILE_ATTRIBUTE_DIRECTORY) == WinAPI.FILE_ATTRIBUTE_DIRECTORY)
return true;
else
return false;
}<br>
private void InitWinAPI()
{
//CommonDef.SetLastError(null);
//WinAPI.SetLastError(0);
}<br>
private string GetWin32Error()
{
//int nErrorCode = Marshal.GetLastWin32Error();
//if (nErrorCode == 0)
// return null;
//return (new Win32Exception(nErrorCode)).Message;
return string.Empty;
}
}
}
------ For reference only -------------------- -------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;<br><br>
namespace SimpleFtp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}<br>
private void btConnect_Click(object sender, EventArgs e)
{
FtpAccess ftpp = new FtpAccess();
ftpp.IP = "192.168.123.161";
ftpp.Port = 21;
ftpp.UserName = "zhou";
ftpp.Password = "";<br>
if (ftpp.Connect())
{
try
{
if (ftpp.Upload("\\DiskOnChip\\asd.jpg", "\\asd.jpg"))
{
MessageBox.Show("上传成功...");
ftpp.CloseConnection();
}
else
{
MessageBox.Show("下载文件失败...");
ftpp.CloseConnection();
return;
}
}
catch (Exception ex)
{
MessageBox.Show("下载文件失败..."+ex.ToString());
ftpp.CloseConnection();
return ;
}
}
else
{
MessageBox.Show("FTP连接失败...");
}
ftpp.CloseConnection();
}<br>
private void button1_Click(object sender, EventArgs e)
{
FtpAccess ftpp = new FtpAccess();
ftpp.IP = "192.168.123.161";
ftpp.Port = 21;
ftpp.UserName = "zhou";
ftpp.Password = "";
//bool ConnectStates =ftpp.Connect() ;
if (ftpp.Connect())
//if(ConnectStates ==true)
{
try
{
if (ftpp.Download("\\EmployeeTxt.txt", "\\DiskOnChip\\EmployeeTxt.txt"))
{
MessageBox.Show("下载文件成功...");
ftpp.CloseConnection();
}
else
{
MessageBox.Show("下载文件失败...");
ftpp.CloseConnection();
return;
}
}
catch(Exception ex)
{
MessageBox.Show("下载文件失败..."+ex.ToString());
ftpp.CloseConnection();
return;
}
}
else
{
MessageBox.Show("FTP连接失败...");
}
//ftpp.CloseConnection();
}
}
}
------ For reference only ------------- --------------------------
good !
I have not used , just turn up .
------ For reference only -------------------------------------- -
very good
------ For reference only ------------------------------- --------
learn Oh
------ For reference only ---------------------- -----------------
good, to branches not ?
------ For reference only -------------------------------------- -
how kind it to the sub ?
------ For reference only -------------------------------------- -
how kind it to the sub ? ? ? ? ? ? ? ? ? ? ? ?
------ For reference only -------------------------------------- -
good things ! ! Top
------ For reference only ------------------------------------- -
good good things ah
------ For reference only ----------------------------- ----------
good, you can learn
------ For reference only --------------------- ------------------
------ For reference only ---------------------------------- -----
thank you for sharing
------ For reference only --------------------------- ------------
Thank you very much !
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------- -----
indicates read 0.0
------ For reference only ------- --------------------------------
learning
------ For reference only - -------------------------------------
------ For reference only ---------------------------------- -----
this code, I've used many of my friends asked me to be passed before qq , even after they get are used properly
not try no right to speak !
practice is the sole criterion for finishing !
------ For reference only -------------------------------------- -
has not come , only to find that you are in the two posts in such remarks ,
I ask you to seriously look at the program yet ?
program called SetLastError (int dwErrCode) this function?
if you please carefully read someone else's program , and truly take your own debugging, and re- evaluation of it !
------ For reference only ---------------------------------- -----
good !
I have not used , just turn up .
Moderators ( colleagues ) you ( you ) a favor , I hope you ( you ) get to try this code ,
correct based on the results and then publish the remarks !
avoid some random evaluation of others , to improve their reputation .
------ For reference only -------------------------------------- -
support the provision of the code of friends .
landlord provided code I wince5.0 +. netframework3.5 through debugging , the normal use , without any problems , code directly take over, their configuration can ftp account password .
------ For reference only -------------------------------- -------
good things to learn ! !
------ For reference only -------------------------------------- -
worth learning to learn. . . . . . . . . . . . . . . . . . .
------ For reference only -------------------------------------- -
my super ah ah
------ For reference only -------------------- -------------------
------ For reference only ---------------------------------- -----
do not understand god Madongdong Oh
------ For reference only ------------------ ---------------------
support the landlord good good
------ For reference only ------ ---------------------------------
------ For reference only ---------------------------------- -----
thank you for sharing
------ For reference Thank you for sharing only ---------------------------------------
thank you for sharing < br> ------ For reference only --------------------------------------- Thank you for sharing
thank you for sharing thank you for sharing
------ For reference only -------------------------- -------------
good good thing ah
------ For reference only ---------------- -----------------------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
nice post . Dingdingding
------ For reference only ----------------------------------- ----
landlord can not achieve WINCE environment
------ For reference only ---------------------------------------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
good is good , worthy of praise a
------ For reference only ----------------------- ----------------
upstairs when he said did not try not to say people will not do it! Each have their own good
------ For reference only ----------------------------- ----------
are looking for information in this regard . . .
------ For reference only -------------------------------------- -
would like to ask how to obtain the specified FTP folder all the file names ?
没有评论:
发表评论