How to clear c # text file contents

I want to create a text file 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 it doesn't open anywhere

please help me thanks

+2
source share
5 answers

This is because you create a StreamWriter and then use File.WriteAllText . The file is already available using StreamWriter .

File.WriteAllText does just that, writes the entire line that you pass to the file. StreamWriter not required if you intend to use File.WriterAllText .

If you do not want to overwrite the 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 adds to the file, you can do this (from this answer ):

 using(StreamWriter sw = File.AppendText(path)) { tw.WriteLine(text); } 
+10
source

You can use StreamWriter to create a file for recording and use Truncate for recording with clearing the previous contents.

 StreamWriter writeFile; writeFile = new StreamWriter(new IsolatedStorageFileStream(filename, FileMode.Truncate, myIsolatedStorage)); writeFile.WriteLine("String"); writeFile.Close(); 

This is using FileMode.Truncate

Truncate Indicates that an existing file that it should open and then truncated so that its size is zero.

+1
source

Assuming your file already exists and you want to clear its contents before filling it or something else, I found a better way to do this with StreamWriter.

 // this line does not create test.txt file, assuming that it already exists, it will remove the contents of test.txt Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(Path.GetFullPath(C:\test.txt), False) // this line will now be inserted into your test.txt file sw.Write("hey there!") 
+1
source

The problem is that you lock the file by initializing the StreamWriter to filePath , and then try to call File.WriteAllText , which also internally tries to lock the file and ultimately gets an exception.

Also, from what it looks like, you are trying to clear the contents of the file, and then write something.
Consider the following:

 private void writeTextFile(string filePath, string text) { using (StreamWriter tw = new StreamWriter(filePath, false)) //second parameter is `Append` and false means override content tw.WriteLine(text); } 
0
source
 // I decided to use this solution // this section is to clear MyFile.txt using(StreamWriter sw = new StreamWriter(@"MyPath\MyFile.txt", false)) { foreach(string line in listofnames) { sw.Write(""); // change WriteLine with Write } sw.Close(); } // and this section is to copy file names to MyFile.txt using(StreamWriter file = new StreamWriter(@"MyPath\MyFile.txt", true)) { foreach(string line in listofnames) { file.WriteLine(line); } } 
0
source

All Articles