将文本附加到 FTP 而不是覆盖
Appending text to FTP instead of overwriting
我正在尝试 追加 一行文本到在线 txt 文件,但我试过的代码正在替换文件中的所有 txt 而不是添加新行,即使在添加 Enviorment.Newline
.
之后
这是我尝试过的方法:
private void submithsbtn_Click(object sender, EventArgs e)
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
string peak = peakvaluelbl.Text;
string dir = @"ftp://example.com/file.txt";
string name = nametbox.Text;
client.UploadString(dir, name + " recived a peak score of: " + peak + Environment.NewLine);
}
有谁知道我怎样才能达到这个结果?
使用高级 webClient Class 对于您想做的事情来说太抽象了。
这ftpWebRequest example实现你想做的。
private static void AppendString(Uri target,byte[] data) {
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.AppendFile;
request.ContentLength = data.length;
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse) request.GetResponse();
response.Close();
}
有一个重载方法 WebClient UploadString 接受三个参数,第二个参数用于指定方法。
client.UploadString(url,"APPE",data);
从 FTP 命令列表中检查 on Wikipedia。
我正在尝试 追加 一行文本到在线 txt 文件,但我试过的代码正在替换文件中的所有 txt 而不是添加新行,即使在添加 Enviorment.Newline
.
这是我尝试过的方法:
private void submithsbtn_Click(object sender, EventArgs e)
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
string peak = peakvaluelbl.Text;
string dir = @"ftp://example.com/file.txt";
string name = nametbox.Text;
client.UploadString(dir, name + " recived a peak score of: " + peak + Environment.NewLine);
}
有谁知道我怎样才能达到这个结果?
使用高级 webClient Class 对于您想做的事情来说太抽象了。
这ftpWebRequest example实现你想做的。
private static void AppendString(Uri target,byte[] data) {
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.AppendFile;
request.ContentLength = data.length;
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse) request.GetResponse();
response.Close();
}
有一个重载方法 WebClient UploadString 接受三个参数,第二个参数用于指定方法。
client.UploadString(url,"APPE",data);
从 FTP 命令列表中检查 on Wikipedia。