FTP FtpWebRequest

周药师 2008-11-28 06:09:52
wince下FTP
用FtpWebRequest、 WebRequestMethods
已经添加了using System.Net;
还是提示:
找不到类型或命名空间名称“FtpWebRequest”(是否缺少 using 指令或程序集引用?)

想问问大家wince下FTP是怎么实现的?
做客户端能下载文件
或者做服务器也好
只要接收到文件都行...
...全文
1237 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
zw20105 2012-07-13
  • 打赏
  • 举报
回复
也在纠结这个问题
wu_kehong 2011-08-18
  • 打赏
  • 举报
回复
学习了,我也试一试
一国之军 2010-03-09
  • 打赏
  • 举报
回复
学习了。
我也正被同样的问题困扰呢。
searcher2000 2009-05-31
  • 打赏
  • 举报
回复
学习了。
我也正被同样的问题困扰呢。
不知道楼主现在问题解决了没有?
net5i 2008-11-29
  • 打赏
  • 举报
回复
那搂主只能用这两个WinAPI实现上传下载了,估计在WinCE下,这些API也是通用的

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpGetFile(IntPtr hFtpSession, string lpszRemoteFile, string lpszNewFile, bool fFailIfExists, int dwFlagsAndAttributes, int dwFlags, int dwContext);

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool FtpPutFile(IntPtr hFtpSession, string lpszLocalFile, string lpszRemoteFile, int dwFlags, int dwContext);

周药师 2008-11-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 net5i 的回复:]
噢,意思是换个高版本的compact framework,而不是操作系统
[/Quote]
wince系统里面的compact framework 我是动不了的
net5i 2008-11-29
  • 打赏
  • 举报
回复
噢,意思是换个高版本的compact framework,而不是操作系统
周药师 2008-11-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 net5i 的回复:]
搂主,FtpWebRequest类已经可以实现你的需求了

你可以换个版本试试
[/Quote]

如何换版本?
是把wince5.0的系统换掉,机器买的系统已经做好的,我是动不了系统的?
还是怎了么办?
qinhl99 2008-11-29
  • 打赏
  • 举报
回复
Unfortunately this feature although supported in .NET Framework 3.5, 3.0, 2.0, it is not supported on the Windows CE platform unlike the WebRequest Class in System.Net.

Platforms
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98



The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information
.NET Framework
Supported in: 3.5, 3.0, 2.0

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx
如果要看在其它os上的例子,文中也有!
net5i 2008-11-29
  • 打赏
  • 举报
回复
CommonDef这个类,搂主不必关心的,和FTP无关
InitWinAPI这个方法,搂主也不必关心的,可以去掉

ResExists等方法是自定义方法
这个方法是用来判断远程FTP文件是否存在的

qinhl99 2008-11-29
  • 打赏
  • 举报
回复
up
周药师 2008-11-29
  • 打赏
  • 举报
回复
谢谢楼上的慷慨!
我给你再加100分...

CommonDef、ResExists、InitWinAPI()...
太多无法解析的东西?
能不能具体点,或者方面的话qq聊?
我的qq 15974748
net5i 2008-11-29
  • 打赏
  • 举报
回复
上传和下载操作前后,必须打开和关闭FTP会话,代码是:

/// <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();

if (this.m_szIP == null || this.m_szUserName == null || this.m_szPassword == null)
{
CommonDef.SetLastError("FTP站点连接失败!FTP访问参数未设置");
return false;
}

this.m_hInternet = WinAPI.InternetOpen("CDATransfer", 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;
}

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;
}

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;
}
}
net5i 2008-11-29
  • 打赏
  • 举报
回复
下面是我曾经写的代码,搂主可以参考:

/// <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;

if (!File.Exists(szLocalFile))
{
CommonDef.SetLastError(string.Format("文件{0}上传失败!本地文件不存在!", szLocalFile));
return false;
}

if (this.ResExists(szRemoteFile, false))
{
string szErrorInfo = string.Format("文件{0}上传到{1}失败!远程文件已经存在!"
, szLocalFile, szRemoteFile);
CommonDef.SetLastError(szErrorInfo);
return false;
}

this.InitWinAPI();
if (!WinAPI.FtpPutFile(this.m_hConnect, szLocalFile, szRemoteFile, WinAPI.FTP_TRANSFER_TYPE_BINARY, 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
{
CommonDef.DeleteFile(szLocalFile, true);
}
catch (Exception ex)
{
string szErrorInfo = string.Format("文件{0}下载到文件{1}失败!无法删除本地已存在文件"
, szRemoteFile, szLocalFile);
CommonDef.SetLastError(szErrorInfo + ex.Message);
return false;
}

if (!this.ResExists(szRemoteFile, false))
{
CommonDef.SetLastError(string.Format("文件{0}下载失败!远程文件不存在!", szRemoteFile));
return false;
}

this.InitWinAPI();
if (!WinAPI.FtpGetFile(this.m_hConnect, szRemoteFile, szLocalFile, false, 0, WinAPI.FTP_TRANSFER_TYPE_BINARY, 0))
{
string szErrorInfo = string.Format("文件{0}下载到文件{1}失败!", szRemoteFile, szLocalFile);
CommonDef.SetLastError(szErrorInfo + this.GetWin32Error());
return false;
}
if (!File.Exists(szLocalFile))
{
string szErrorInfo = string.Format("文件{0}下载到文件{1}失败!", szRemoteFile, szLocalFile);
CommonDef.SetLastError(szErrorInfo);
return false;
}
return true;
}
周药师 2008-11-29
  • 打赏
  • 举报
回复
楼上的确实是一个好方法
但找了很多资料 不知道怎么实现;
bool i = FtpClient.FtpGetFile(hConnection, "\\aa.txt", "\\aa.txt", true, 0, 1,sss);
if(i)
MessageBox.Show("下载文件成功!");

其中参数hConnection 、context 是system.IntPtr的;
不知道如何定义带入这个两个参数...

能不能给个下载的例子?
mjjzg 2008-11-28
  • 打赏
  • 举报
回复
没在Windows CE下搞过,应该是不支持的
net5i 2008-11-28
  • 打赏
  • 举报
回复
下面贴子里面我贴过一些代码,是Win32下的,搂主可以参考参考
http://topic.csdn.net/u/20081118/15/1ce94d48-1645-48e5-89a4-e88153809f83.html
net5i 2008-11-28
  • 打赏
  • 举报
回复
搂主,FtpWebRequest类已经可以实现你的需求了

你可以换个版本试试
WG_Wolf 2008-11-28
  • 打赏
  • 举报
回复
呵呵!
不懂
加载更多回复(6)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧