Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear text file content c#

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

like image 529
user3281649 Avatar asked Feb 14 '14 22:02

user3281649


4 Answers

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);
}
like image 106
Codeman Avatar answered Oct 05 '22 11:10

Codeman


You can use StreamWriter for creating a file for write and use Truncate to write with clearing previous content.

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

This use FileMode.Truncate

Truncate Specifies that an existing file it to be opened and then truncated so that its size is zero bytes.

like image 44
Harsha Siriwardana Avatar answered Oct 05 '22 13:10

Harsha Siriwardana


Assuming that your file already exists and you want to clear its contents before populating it or whatever, I found the best way to do this with StreamWriter is..

// 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!")
like image 28
Albert Cosme Monteiro Avatar answered Oct 05 '22 13:10

Albert Cosme Monteiro


// 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);
        }
      }
like image 41
RobertoFRey Avatar answered Oct 05 '22 12:10

RobertoFRey