Remove all previous text before writing
Asked Answered
S

6

14

I am writing a text file and each time i write i want to clear the text file.

try
{
    string fileName = "Profile//" + comboboxSelectProfile.SelectedItem.ToString() + ".txt";
    using (StreamWriter sw = new StreamWriter(("Default//DefaultProfile.txt").ToString(), true))
    {
        sw.WriteLine(fileName);
        MessageBox.Show("Default is set!");
    }
    DefaultFileName = "Default//DefaultProfile.txt";
}
catch 
{ 
}

How do I do this? I want to remove all previous content from DefaultProfile.txt.

I actually have to know the method or way (just a name could be) to remove all content from the text file.

Sincere answered 28/12, 2012 at 15:3 Comment(2)
A easy way could be to get the data, delete the file and create a new one. Is there any better way?Sincere
I think this is a simpler [Solution][1] [1]: https://mcmap.net/q/826920/-how-to-clear-text-file-content-cDulcle
H
24

You could just write an empty string to the existing file:

File.WriteAllText(@"Default\DefaultProfile.txt", string.Empty);

Or change the second parameter in the StreamWriter constructor to false to replace the file contents instead of appending to the file.

Highmuckamuck answered 28/12, 2012 at 15:7 Comment(0)
P
18

You can look at the Truncate method

FileInfo fi = new FileInfo(@"Default\DefaultProfile.txt");
using(TextWriter txtWriter = new StreamWriter(fi.Open(FileMode.Truncate)))
{
    txtWriter.Write("Write your line or content here");
}
Pharmaceutical answered 28/12, 2012 at 15:11 Comment(0)
F
6

The most straightforward and efficient technique is to use the StreamWriter constructor's boolean parameter. When it's set to false it overwrites the file with the current operation. For instance, I had to save output of a mathematical operation to a text file. Each time I wanted ONLY the current answer in the text file. So, on the first StreamWriter operation, I set the boolean value to false and the subsequent calls had the bool val set to true. The result is that for each new operation, the previous answer is overwritten and only the new answer is displayed.

        int div = num1 / denominator;
        int mod = num1 % denominator;
        Console.Write(div);
        using (StreamWriter writer = new StreamWriter(FILE_NAME, false ))
        {
            writer.Write(div);
        }
        Console.Write(".");
        using (StreamWriter writer = new StreamWriter(FILE_NAME, true))
        {
            writer.Write(".");
        }       
Factional answered 4/5, 2014 at 2:52 Comment(0)
A
3

You can use FileMode.Truncate. Code will look like

FileStream fs = new 
FileStream(filePath, FileMode.Truncate, FileAccess.Write )
{  
  fs.Close();
}
Antiknock answered 13/4, 2017 at 4:22 Comment(0)
C
2

Simply change the second parameter from true to false in the contructor of StreamWriter.

using (StreamWriter sw = new StreamWriter(("Default//DefaultProfile.txt").ToString(), **false**))

See StreamWriter Contructor

Cripps answered 10/9, 2018 at 9:19 Comment(0)
S
1

System.IO.File.Delete, or one of the System.IO.FileStream constructor overloads specifying FileMode.Create

Servia answered 28/12, 2012 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.