求一个从FTP下载指定文件到本地计算机的C#代码。
我想从远程服务器上下载一个XML文件,放到本地计算机的一个指定的目录下,比如:C:\update请教各位高手,这样的代码用C#怎么写,给个具体的方法就可以了,谢谢...
我想从远程服务器上下载一个XML文件,放到本地计算机的一个指定的目录下,比如:C:\update 请教各位高手,这样的代码用C#怎么写,给个具体的方法就可以了,谢谢
展开
展开全部
#region 下载文件
/**//// <summary>
/// 从FTP服氏腔务器下载文件,使用与远程文件同名的文件名来保存文件
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
/// <param name="LocalPath">本地路径</param>
public bool DownloadFile(string RemoteFileName, string LocalPath)
{
return DownloadFile(RemoteFileName, LocalPath, RemoteFileName);
}
/**//// <summary>
/// 从FTP服务器下载文件,指定本地路径和本地文件名
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
/// <param name="LocalPath">本地路径</param>
/// <param name="LocalFilePath">保存文件的本地路径,后面带有"\"</param>
/// <param name="LocalFileName">保存本地的文件名</param>
public bool DownloadFile(string RemoteFileName, string LocalPath, string LocalFileName)
{
byte[] bt = null;
try
{
if (!IsValidFileChars(RemoteFileName) || !IsValidFileChars(LocalFileName) || !IsValidPathChars(LocalPath))
{
throw new Exception("非法文件名或目录名!");
}
if (!Directory.Exists(LocalPath))
{
throw new Exception("本地文件路径不稿誉存在!");
}
string LocalFullPath = Path.Combine(LocalPath, LocalFileName);
if (File.Exists(LocalFullPath))
{
throw new Exception("当前路径下已经存在键核段同名文件!");
}
bt = DownloadFile(RemoteFileName);
if (bt != null)
{
FileStream stream = new FileStream(LocalFullPath, FileMode.Create);
stream.Write(bt, 0, bt.Length);
stream.Flush();
stream.Close();
return true;
}
else
{
return false;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
/**//// <summary>
/// 从FTP服务器下载文件,返回文件二进制数据
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
public byte[] DownloadFile(string RemoteFileName)
{
try
{
if (!IsValidFileChars(RemoteFileName))
{
throw new Exception("非法文件名或目录名!");
}
Response = Open(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.DownloadFile);
Stream Reader = Response.GetResponseStream();
MemoryStream mem = new MemoryStream(1024 * 500);
byte[] buffer = new byte[1024];
int bytesRead = 0;
int TotalByteRead = 0;
while (true)
{
bytesRead = Reader.Read(buffer, 0, buffer.Length);
TotalByteRead += bytesRead;
if (bytesRead == 0)
break;
mem.Write(buffer, 0, bytesRead);
}
if (mem.Length > 0)
{
return mem.ToArray();
}
else
{
return null;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
/**//// <summary>
/// 从FTP服氏腔务器下载文件,使用与远程文件同名的文件名来保存文件
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
/// <param name="LocalPath">本地路径</param>
public bool DownloadFile(string RemoteFileName, string LocalPath)
{
return DownloadFile(RemoteFileName, LocalPath, RemoteFileName);
}
/**//// <summary>
/// 从FTP服务器下载文件,指定本地路径和本地文件名
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
/// <param name="LocalPath">本地路径</param>
/// <param name="LocalFilePath">保存文件的本地路径,后面带有"\"</param>
/// <param name="LocalFileName">保存本地的文件名</param>
public bool DownloadFile(string RemoteFileName, string LocalPath, string LocalFileName)
{
byte[] bt = null;
try
{
if (!IsValidFileChars(RemoteFileName) || !IsValidFileChars(LocalFileName) || !IsValidPathChars(LocalPath))
{
throw new Exception("非法文件名或目录名!");
}
if (!Directory.Exists(LocalPath))
{
throw new Exception("本地文件路径不稿誉存在!");
}
string LocalFullPath = Path.Combine(LocalPath, LocalFileName);
if (File.Exists(LocalFullPath))
{
throw new Exception("当前路径下已经存在键核段同名文件!");
}
bt = DownloadFile(RemoteFileName);
if (bt != null)
{
FileStream stream = new FileStream(LocalFullPath, FileMode.Create);
stream.Write(bt, 0, bt.Length);
stream.Flush();
stream.Close();
return true;
}
else
{
return false;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
/**//// <summary>
/// 从FTP服务器下载文件,返回文件二进制数据
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
public byte[] DownloadFile(string RemoteFileName)
{
try
{
if (!IsValidFileChars(RemoteFileName))
{
throw new Exception("非法文件名或目录名!");
}
Response = Open(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.DownloadFile);
Stream Reader = Response.GetResponseStream();
MemoryStream mem = new MemoryStream(1024 * 500);
byte[] buffer = new byte[1024];
int bytesRead = 0;
int TotalByteRead = 0;
while (true)
{
bytesRead = Reader.Read(buffer, 0, buffer.Length);
TotalByteRead += bytesRead;
if (bytesRead == 0)
break;
mem.Write(buffer, 0, bytesRead);
}
if (mem.Length > 0)
{
return mem.ToArray();
}
else
{
return null;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询