我不知道下载的文件到底存放在那里的 请高手指教一下 谢谢下面的代码示例演示如何使用 WebClient 类从 FTP 服务器下载文件。
Uri uri = new Uri("ftp://172.21.0.66/新建 BMP 图像.bmp");
public static bool DisplayFileFromServer(Uri serverUri)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
WebClient request = new WebClient();
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","[email protected]");
try
{
byte [] newFileData = request.DownloadData (serverUri.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
Console.WriteLine(fileString);
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
return true;
}
Uri uri = new Uri("ftp://172.21.0.66/新建 BMP 图像.bmp");
public static bool DisplayFileFromServer(Uri serverUri)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
WebClient request = new WebClient();
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","[email protected]");
try
{
byte [] newFileData = request.DownloadData (serverUri.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
Console.WriteLine(fileString);
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
return true;
}
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
byte [] newFileData = request.DownloadData (serverUri.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
Console.WriteLine(fileString);
特别是最后这一句,这段代码实际上只是个读取远程文件的内容然后在控制台显示出来。
注意:byte [] newFileData = request.DownloadData (serverUri.ToString());
换为:byte [] newFileData = request.DownloadFile (serverUri.ToString());
再去试试吧