Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中使用ftp下载文件#_C#_Ftp - Fatal编程技术网

C# 在c中使用ftp下载文件#

C# 在c中使用ftp下载文件#,c#,ftp,C#,Ftp,作为一名初级开发人员,我应该找到一个使用ftp下载文件的解决方案,我有这段代码。 它可以工作,但有时,我无法打开下载的文件 public static bool DownloadDocument(string ftpPath, string downloadPath) { bool retVal = false; try { Uri serverUri = new Uri(ftpPath); if (serverUri.Scheme != Uri.UriSchemeFtp

作为一名初级开发人员,我应该找到一个使用ftp下载文件的解决方案,我有这段代码。
它可以工作,但有时,我无法打开下载的文件

public static bool DownloadDocument(string ftpPath, string downloadPath) {
  bool retVal = false;
  try {
    Uri serverUri = new Uri(ftpPath);
    if (serverUri.Scheme != Uri.UriSchemeFtp) {
        return false;
    }
    FtpWebRequest reqFTP;
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
    reqFTP.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
    reqFTP.KeepAlive = false;
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    reqFTP.UseBinary = true;
    reqFTP.Proxy = null;
    reqFTP.UsePassive = false;

    using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) {
      using (Stream responseStream = response.GetResponseStream()) {
        using (FileStream writeStream = new FileStream(downloadPath, FileMode.Create)) {
          int Length = 1024 * 1024 * 30;
          Byte[] buffer = new Byte[Length];
          responseStream.Read(buffer, 0, Length);
        }
      }
    }
    retVal = true;
  }
  catch (Exception ex) {
    //Error logging to add
  }

  return retVal;
}
有什么想法吗

你为什么不用它呢?。由Microsoft实施,用于从FTP下载

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("log", "pass");
    client.DownloadFile("ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

}

检查无法打开的文件是否已损坏。 例如,ftp和本地pc上的文件大小是否相同

你应该检查你的读者是否读到了结尾

public static bool DownloadDocument(string ftpPath, string downloadPath)
{
    bool retVal = false;
    try
    {
        Uri serverUri = new Uri(ftpPath);
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
        FtpWebRequest reqFTP;
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
        reqFTP.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.Proxy = null;
        reqFTP.UsePassive = false;

        using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (FileStream writeStream = new FileStream(downloadPath, FileMode.Create))
                {
                    int Length = 1024 * 1024 * 30;
                    Byte[] buffer = new Byte[Length];
                  int byteReads =  responseStream.Read(buffer, 0, Length);
                  while(byteReads > 0)
                  {
                      //Try like this
                      writeStream.Write(buffer, 0, byteReads);
                      bytesRead = responseStream.Read(buffer, 0, Length);
                  }

                }
            }
        }
        retVal = true;
    }
    catch (Exception ex)
    {
       //Error logging to add
    }

    return retVal;
}

你说什么不清楚。我发布了实际代码。我想说的是,有些文件我无法打开。您的代码缺少对输出文件的任何实际写入。所以说“它有时会工作”是完全不正确的。有空的
catch(Exception ex)
块是毫无意义的。即使您对它做了一些事情,捕获常规
异常也已经够糟糕的了。您应该真正避免捕捉这样的错误。
int Length=1024*1024*30也许这就是问题所在。拥有一个静态缓冲区似乎并不好。可能是缓冲区太小,因此文件没有完整写入。我如何才能像这样保存在特殊文件夹中?@Tony_Clark,检查第二个参数。这是本地文件名。我测试了一个有问题的文件,它工作!但另一个解决方案更简单。谢谢。没问题,欢迎光临。