在写入前删除所有先前的文本 [英] Remove all previous text before writing

查看:101
本文介绍了在写入前删除所有先前的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个文本文件,每次我写我想要清除文本文件。

  try 
{
string fileName =Profile //+ comboboxSelectProfile.SelectedItem.ToString +.txt;

使用(StreamWriter sw = new StreamWriter((Default // DefaultProfile.txt)。ToString(),true))
{
sw.WriteLine(fileName);
MessageBox.Show(Default is set!);
}
DefaultFileName =Default // DefaultProfile.txt;
}
catch {}

如何做?我要从 DefaultProfile.txt 中删除所有以前的内容。



我实际上必须知道方法或方法(只是名称可以)从文本文件中删除所有内容。

  

> File.WriteAllText(@Default\DefaultProfile.txt,String.Empty);

或更改 StreamWriter > 替换文件内容,而不是添加到文件


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.

解决方案

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.

这篇关于在写入前删除所有先前的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆