我试图 在线添加 一行文字到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);
}
有谁知道我怎么能够达到这个结果?
答案 0 :(得分:5)
使用高级webClient类对于您想要做的事情来说太抽象了。
这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();
}
答案 1 :(得分:2)
有一个重载方法WebClient UploadString,它接受三个参数,第二个是指定方法。
client.UploadString(url,"APPE",data);
从FTP命令列表on Wikipedia检查。