2

注意:我不能使用 FileMode.Create 或 FileMode.Truncate,因为它们会导致一些不必要的问题

 byte[] data = new UTF8Encoding().GetBytes(this.Box.Text);
                FileStream f = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
f.Write(data, 0, data.Length);
                f.Close();

这会将新文本附加到旧文本的顶部。我怎样才能覆盖所有内容?

4

1 回答 1

0

你的代码

byte[] data = new UTF8Encoding().GetBytes(this.Box.Text); FileStream f = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); f.Write(data, 0, data.Length); f.Close();

你怎么不能做这样的事情..?请解释为什么你不能使用FileMode.Create

byte[] data = new UTF8Encoding().GetBytes(this.Box.Text);
using(Stream f = File.Open(path, FileMode.Write)) 
{
   f.Write(data, 0, data.Length);
}

您还可以执行以下操作

1. Let the users write to the same file
2. capture the users Machine Name or User Id then
2. write a line in your file like this 

strSeprate = new string('*',25); 
//will write to the file "*************************";
f.Write(strSeprate);
f.Write(Machine Name or UserId);
f.Write(data);
f.Write(DateTime.Now.ToString());
f.Write(strSeprate);

只是一个想法..

于 2013-01-11T01:16:42.777 回答