Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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# 从FTP服务器随机读取返回一个空白文本文件_C#_Download_Ftp_Text Files - Fatal编程技术网

C# 从FTP服务器随机读取返回一个空白文本文件

C# 从FTP服务器随机读取返回一个空白文本文件,c#,download,ftp,text-files,C#,Download,Ftp,Text Files,首先,我确实试图为我的问题寻找答案,但没有找到答案 我一直在制作一个程序,在这个程序中,我的本地文本文件和位于FTP服务器中的在线文本文件会自动相互更新。文本文件包含名称列表。此应用程序设计为可由不同的计算机同时使用,并可以在列表中添加和删除名称 借助额外的算法,我能够合并本地和在线文本文件,即使在脱机使用时也不会造成问题,并且在有internet连接时(使用额外的本地文件)只进行更新 现在,对于这个问题,每件事都能在90%的时间里完美地工作。但是,有时从FTP服务器下载的文本文件返回一个空白文

首先,我确实试图为我的问题寻找答案,但没有找到答案

我一直在制作一个程序,在这个程序中,我的本地文本文件和位于FTP服务器中的在线文本文件会自动相互更新。文本文件包含名称列表。此应用程序设计为可由不同的计算机同时使用,并可以在列表中添加和删除名称

借助额外的算法,我能够合并本地和在线文本文件,即使在脱机使用时也不会造成问题,并且在有internet连接时(使用额外的本地文件)只进行更新

现在,对于这个问题,每件事都能在90%的时间里完美地工作。但是,有时从FTP服务器下载的文本文件返回一个空白文本文件,即使它不是。我注意到,当我的应用程序很难检查互联网连接时(ping google.com),这种情况最有可能发生。这将导致列表被完全删除,因为我的应用程序会将其解释为“使用该应用程序的其他用户删除了列表”

以下是我从FTP服务器下载的方法:

public Boolean DownloadFile(string uri, string path)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
    request.UsePassive = true;
    request.KeepAlive = true;
    request.UseBinary = true;
    request.Proxy = null;
    request.Timeout = 5000;
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.Credentials = new NetworkCredential(this.username, this.password);
    try
    {
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        StringBuilder sb = new StringBuilder();
        sb.AppendLine(reader.ReadToEnd());
        File.WriteAllText(path, sb.ToString());
        reader.Close();
        response.Close();
        return true;
    }
    catch (Exception e )
    {
        MessageBox.Show("FTPHANDLER DOWNLOAD FILE:"+e.ToString());
        return false;
    }
}
我用于检查internet连接的方法:

private static bool CheckForInternetConnection()
    {
        try
        {
            Ping myPing = new Ping();
            String host = "google.com";
            byte[] buffer = new byte[32];
            int timeout = 15000;
            PingOptions pingOptions = new PingOptions();
            PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
            return (reply.Status == IPStatus.Success);
        }
        catch (Exception e)
        {
            //MessageBox.Show(e.ToString());
            return false;
        }
    }
这就是我所说的。我已经提供了安全措施,防止这种情况发生,但它仍然会发生

public void Update()
{ 
  if(CheckForInternetConnection()){
     if (DownloadFile(uri,path))
     {
       char[] charsToTrim = { '\r', '\n' };
       string onlineFile = File.ReadAllText(path).TrimEnd(charsToTrim);
       if (onlineConfigFile.Equals("") || onlineConfigFile == null)
           MessageBox.Show("Downloaded file is empty");
       //still do stuff here regardless of the message
       //If the other user really do intend to delete the list
     }
   }
}
update方法在不同的线程中执行


至于我在这里的第一篇文章,很抱歉它太长了。我确实试着把它缩短,但不想错过任何细节。

由于我的问题的性质(随机),我认为“锁定”解决方案解决了我的问题,因为在我使用它一段时间后,问题没有发生。这可能是因为线程每5秒产生一次,导致了它们同时处理方法的不同部分的问题

以下是固定代码:

public void Update()
{ 
  lock(lockObject)
  {
    if(CheckForInternetConnection())
    {
      if (DownloadFile(uri,path))
      {
        char[] charsToTrim = { '\r', '\n' };
        string onlineFile = File.ReadAllText(path).TrimEnd(charsToTrim);
        if (onlineConfigFile.Equals("") || onlineConfigFile == null)
          MessageBox.Show("Downloaded file is empty");
       //still do stuff here regardless of the message
       //If the other user really do intend to delete the list
      }
    }
  }
}
我将“lockObject”声明为数据类型为“Object”的类级变量。

我现在考虑使用“lock”,因为更新方法每5秒调用一次,并且每个更新方法都在不同的线程中运行。这将确保安全措施的逻辑不会被其他线程利用。