Aby wyczyścić zawartość pliku

Muszę wyczyścić zawartość konkretnego pliku przy każdym uruchomieniu aplikacji. Jak to zrobić?

Author: Anatoliy Nikolaev, 2011-02-15

5 answers

Możesz użyć pliku .Metoda WriteAllText .

System.IO.File.WriteAllText(@"Path/foo.bar",string.Empty);
 100
Author: The Scrum Meister,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-15 04:52:24

To właśnie zrobiłem, aby wyczyścić zawartość pliku bez tworzenia nowego pliku ponieważ nie chciałem, aby plik wyświetlał nowy czas tworzenia, nawet gdy aplikacja właśnie zaktualizowała jego zawartość.

FileStream fileStream = File.Open(<path>, 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.
 65
Author: Abhay Jain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-07 14:03:24

Użyj FileMode.Obcinaj za każdym razem, gdy tworzysz plik. Umieść również plik.Utwórz wewnątrz spróbuj złapać.

 10
Author: sajoshi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-15 05:11:18

Najprostszym sposobem na to jest usunięcie pliku za pomocą aplikacji i utworzenie nowego o tej samej nazwie... w jeszcze prostszy sposób wystarczy, aby aplikacja nadpisała go nowym plikiem.

 2
Author: Kartikya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-15 04:57:33

Spróbuj użyć czegoś takiego jak

Plik.Create

Tworzy lub nadpisuje plik w określona ścieżka.

 1
Author: Adriaan Stander,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-15 04:52:47