I suppose my title isn't that clear.
I'll try to explain:
I can write and read a file using a FileStream
FileStream fs = new FileStream("C:\\Users\\Public\\text.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
private void button1_Click(object sender, EventArgs e)
{
fs.Seek(0,0);
StreamReader sr = new StreamReader(fs);
textbox.Text = sr.ReadToEnd();
}
private void button2_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(textbox.Text);
sw.Flush();
}
This way other programs can't use the file, but I also can't delete content. Writing to it only adds the string, it doesn't replace the content.
Or I can do it without a FileStream:
private void button1_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("C:\\Users\\Public\\text.txt");
textBox1.Text = sr.ReadToEnd();
sr.Close();
}
private void button2_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter("C:\\Users\\Public\\text.txt", false);
sw.Write(textBox1.Text);
sw.Close();
}
This way, the content of the file is replaced, but it has no lock on the files.
But I want both. What is the solution?