首页 > 解决方案 > C# 代码从 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 服务器上的设置或环境。

请建议。

标签: c#.netiisftp

解决方案


推荐阅读