读取FTP文件 [英] read file from ftp

查看:168
本文介绍了读取FTP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读从FTP服务器上的文件,而不将其下载到本地文件。我写了一个功能,但它不工作:

 私人字符串GetServerVersion()
{
Web客户端请求=新的Web客户端();

字符串URL = FtpPath +文件名;
字符串版本=;

request.Credentials =新的NetworkCredential(MailLibConfigurations.ftp_user,MailLibConfigurations.ftp_pas);

//request.DownloadFile(new乌里(FtpPath)+文件名,D:\\sdc_version.txt);

{
字节[] = newFileData request.DownloadData(新的URI(FtpPath)+文件名);
串fileString = System.Text.Encoding.UTF8.GetString(newFileData);
}
赶上(引发WebException五)
{
}
返回版本;
}


解决方案

下面是一个使用简单的工作示例您的代码以抓住从Microsoft公共的FTP服务器的文件:

  Web客户端请求=新的WebClient(); 
字符串URL =ftp://ftp.microsoft.com/developr/fortran/+README.TXT;
request.Credentials =新的NetworkCredential(匿名,anonymous@example.com);


{
字节[] = newFileData request.DownloadData(URL);
串fileString = System.Text.Encoding.UTF8.GetString(newFileData);
Console.WriteLine(fileString);
}
赶上(引发WebException五)
{
//做一些诸如日志错误,但是这是基于OP原代码
//所以现在我们没做什么。
}



我觉得你在你的代码在这里失去了在这条线斜线:

 字符串的URL = FtpPath +文件名; 



也许 FtpPath 和<$ C $之间C>文件名?


I want to read a file from a FTP server without downloading it to a local file. I wrote a function but it does not work:

private string GetServerVersion()
{
    WebClient request = new WebClient();

    string url = FtpPath + FileName;
    string version = "";

    request.Credentials = new NetworkCredential(MailLibConfigurations.ftp_user, MailLibConfigurations.ftp_pas);

    //request.DownloadFile(new Uri(FtpPath) + FileName, "D:\\sdc_version.txt");
    try
    {
        byte[] newFileData = request.DownloadData(new Uri(FtpPath)+FileName);
        string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
    }
    catch (WebException e)
    {
    }
    return version;
}

解决方案

Here's a simple working example using your code to grab a file from the Microsoft public FTP servers:

WebClient request = new WebClient();
string url = "ftp://ftp.microsoft.com/developr/fortran/" +"README.TXT";
request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com");

try
{
  byte[] newFileData = request.DownloadData(url);
  string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
  Console.WriteLine(fileString);
}
catch (WebException e)
{
  // Do something such as log error, but this is based on OP's original code
  // so for now we do nothing.
}

I reckon you're missing a slash on this line here in your code:

string url = FtpPath + FileName;

Perhaps between FtpPath and FileName ?

这篇关于读取FTP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆