正如我所见,我迟到了一点,但我遇到了和你一样的问题,在我寻找解决方案之后,我找到了一种无需下载和上传文件即可进行更改的方法。
我的目标是在结束时保存程序执行期间所做的所有更改,并在程序开始时从存储在 FTP 服务器中的 .txt 文件加载这些数据。
下面是使用的两种方法:
public static void Save(ArrayList dataStore)
{
WebClient request = new WebClient();
string url = "ftp://ftpname/directory/" + "file.txt";
request.Credentials = new NetworkCredential("username", "password");
Stream postStream = request.OpenWrite(url);
foreach (Data data in dataStore)
{
byte[] writeData = Encoding.ASCII.GetBytes(data + "#");
postStream.Write(writeData, 0, writeData.Length);
}
}
public static ArrayList Load()
{
ArrayList dataStore = new ArrayList();
WebClient request = new WebClient();
string url = "ftp://ftpname/directory/" + "file.txt";
request.Credentials = new NetworkCredential("username", "password");
byte[] newFileData = request.DownloadData(url);
string fileString = Encoding.UTF8.GetString(newFileData);
if (fileString == "")
{
return dataStore;
}
string[] dataString = fileString.Split('#');
foreach (string data in dataString)
{
if (data != "")
{
dataStore.Add(data);
}
}
return dataStore;
}