c# - 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 服务器上的设置或环境。
请建议。
解决方案
推荐阅读
- google-cloud-platform - 用于创建 Firestore 的 Terraform
- linux - 如何使用 Bluez L2CAP 发送更多字节?
- java - 以编程方式更改旧 android 版本上的应用程序语言
- c# - 如何向地形添加材质?
- android - 使用新项目更新我的回收站视图时遇到问题
- flutter - 验证在谷歌注册上注册的用户的好方法是什么?
- react-native - 如何使反应原生相机的背景变黑?
- swift - 如何在没有绑定的情况下使用 NSOutlineView
- python-3.x - 禁用 TensorFlow 警告的附加信息
- python - 如何在 3D 散点图中添加平面