Clearing content of text file using C#

前端 未结 5 1634
别那么骄傲
别那么骄傲 2020-12-05 01:15

How can I clear the content of a text file using C# ?

相关标签:
5条回答
  • 2020-12-05 02:00

    Another short version:

    System.IO.File.WriteAllBytes(path, new byte[0]);
    
    0 讨论(0)
  • 2020-12-05 02:02

    You can use always stream writer.It will erase old data and append new one each time.

    using (StreamWriter sw = new StreamWriter(filePath))
    {                            
        getNumberOfControls(frm1,sw);
    }
    
    0 讨论(0)
  • 2020-12-05 02:06
     using (FileStream fs = File.Create(path))
     {
    
     }
    

    Will create or overwrite a file.

    0 讨论(0)
  • 2020-12-05 02:18
    File.WriteAllText(path, String.Empty);
    

    Alternatively,

    File.Create(path).Close();
    
    0 讨论(0)
  • 2020-12-05 02:18

    Just open the file with the FileMode.Truncate flag, then close it:

    using (var fs = new FileStream(@"C:\path\to\file", FileMode.Truncate))
    {
    }
    
    0 讨论(0)
提交回复
热议问题