C#代码从FTP获取数据的缓存版本

时间:2019-06-20 08:08:55

标签: c# .net iis ftp

在我的项目中,我正在从IIS7和Linux服务器上创建的ftp下载一些文件,并将其保存到我的Appdata / Roamingfolder中。当我修改csv文件的内容或只是删除旧文件并用具有相同名称但内容已修改的新文件替换它时,问题就来了。

每次我都必须重命名该文件并下载重命名的文件。这表明它正在下载文件的某些缓存映像,而我无法在本地系统以及ftp服务器上找到该文件。

public static bool FTPFileDownload(string strFolderName, string 
pathToStore, bool blIsSingleFile = true, string strFileType = "")
    {
        try
        {
            if (!Directory.Exists(pathToStore))
            {
                // Try to create the directory.
                DirectoryInfo di = Directory.CreateDirectory(pathToStore);
            }
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["FTPUrl"].ToString() + strFolderName);
            request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["FTPUser"].ToString(), ConfigurationManager.AppSettings["FTPPassword"].ToString());
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.Proxy = null;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            StreamReader streamReader = new StreamReader(response.GetResponseStream());
            System.Collections.Generic.List<string> directories = new System.Collections.Generic.List<string>();

            string line = streamReader.ReadLine();
            while (!string.IsNullOrEmpty(line))
            {
                //If extension is available match with extension and add.
                bool blAddFile = false;
                if (!String.IsNullOrEmpty(strFileType))
                {
                    string strExt = Path.GetExtension(ConfigurationManager.AppSettings["FTPUrl"].ToString() + line).Remove(0, 1);
                    if (strExt.ToLower() == strFileType.ToLower())
                        blAddFile = true;
                }
                else
                    blAddFile = true;

                if (blAddFile)
                {
                    directories.Add(line);
                }
                line = streamReader.ReadLine();
            }
            streamReader.Close();

            using (WebClient ftpClient = new WebClient())
            {
                ftpClient.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FTPUser"].ToString(), ConfigurationManager.AppSettings["FTPPassword"].ToString());

                for (int i = 0; i <= directories.Count - 1; i++)
                {
                    if (directories[i].Contains("."))
                    {
                        string path = ConfigurationManager.AppSettings["FTPUrl"].ToString() + strFolderName
                            + (blIsSingleFile ? "" : "/" + directories[i].ToString());

                        string trnsfrpth = pathToStore + directories[i].ToString();
                        ftpClient.DownloadFile(path, trnsfrpth);
                    }
                }
                return true;
            }
        }
        catch (Exception ex)
        {
            FileLogger.logMessage(ex.Message);
            if (FileLogger.IsDebuggingLogEnabled)
            {
                FileLogger.HandleError("FTPFileDownload", ex, "Common Helper Error 4:");
            }
            return false;
        }
    }

我不知道这是怎么回事。我的代码错误,或者是ftp服务器上的设置或环境。

请提出建议。

0 个答案:

没有答案