how to clear text file content c#
c#, streamwriter
Solution
That's because you're creating a `StreamWriter`, then using `File.WriteAllText`. Your File is already being accessed with the `StreamWriter`.
`File.WriteAllText` does just that, writes the entire string you pass to it to a file. `StreamWriter` is unnecessary if you're going to use `File.WriterAllText`.
If you don't care about overwriting an existing file, you can do this:
private void writeTextFile(string filePath, string text)
{
File.WriteAllText(filePath, text);
}
If you want to use `StreamWriter` (which, by the way, `File.WriteAllText` uses, it just hides it), and append to the file, you can do this (from this answer):
using(StreamWriter sw = File.AppendText(path))
{
tw.WriteLine(text);
}
Problem
i want clear text file contet with this method ``` private void writeTextFile(string filePath, string text) { if (!File.Exists(filePath)) { File.Create(filePath).Close(); } using (StreamWriter tw = new StreamWriter(filePath)) { File.WriteAllText(filePath,""); tw.WriteLine(text); tw.Close(); } } ``` but i get this error ``` The process cannot access the file because it is being used by another process. ``` but this not open in anywhere , please help me thank's