How can I clear the content of a file?

后端 未结 6 1748
慢半拍i
慢半拍i 2020-12-02 21:48

I need to clear the contents of a particular file every time the applications starts. How do I do it?

6条回答
  •  春和景丽
    2020-12-02 22:39

    This is what I did to clear the contents of the file without creating a new file as I didn't want the file to display new time of creation even when the application just updated its contents.

    FileStream fileStream = File.Open(, FileMode.Open);
    
    /* 
     * Set the length of filestream to 0 and flush it to the physical file.
     *
     * Flushing the stream is important because this ensures that
     * the changes to the stream trickle down to the physical file.
     * 
     */
    fileStream.SetLength(0);
    fileStream.Close(); // This flushes the content, too.
    

提交回复
热议问题