C# 创建一个Txt文件并写入

C# 创建一个Txt文件并写入,c#,textbox,text-files,C#,Textbox,Text Files,我想创建一个文本文件,然后将文本框的文本添加到其中。 使用以下代码创建文本文件不会出现任何问题: InitializeComponent(); string path = @"C:\Users\Morris\Desktop\test.txt"; if (!File.Exists(path)) { File.Create(path); } 但是,当我尝试将文本添加到文本文件时,出现了一个错误,即正在使用该文件。如果文件在运行代码之前已经存在,我不会得到这个错误,并且TextBox.Tex

我想创建一个文本文件,然后将
文本框的文本添加到其中。
使用以下代码创建文本文件不会出现任何问题:

InitializeComponent();
string path = @"C:\Users\Morris\Desktop\test.txt";
if (!File.Exists(path))
{
    File.Create(path);
}
但是,当我尝试将文本添加到文本文件时,出现了一个错误,即正在使用该文件。如果文件在运行代码之前已经存在,我不会得到这个错误,并且
TextBox.Text
被添加到文件中。 我使用以下代码将文本添加到文本文件:

public void writeTxt()
{
    string path = @"C:\Users\Morris\Desktop\test.txt";
    if (File.Exists(path))
    {
        using (var tw = new StreamWriter(path, true))
        {
            tw.WriteLine(TextBox1.Text);
            tw.Close();
        }
    }
}
您能帮助我吗?

您应该使用with
using
语句,因为它在创建时锁定了文件。所以只需更改此行:

File.Create(path);
为此:

using (File.Create(path));

这个问题以前在这个线程中已经得到了回答。

在再次使用该文件之前,需要关闭该文件的流

您需要移动您的

tw.Close();
在你的使用范围之外。像这样:

public void writeTxt()
{
    string path = @"C:\Users\Morris\Desktop\test.txt";
    if (File.Exists(path))
    {
        using (var tw = new StreamWriter(path, true))
        {
            tw.WriteLine(TextBox1.Text);
        }
        tw.Close();
    }
}
编辑:正如所指出的,当使用结束时,writer被释放,因此不需要手动关闭

public void writeTxt()
{
    string path = @"C:\Users\Morris\Desktop\test.txt";
    if (File.Exists(path))
    {
        using (var tw = new StreamWriter(path, true))
        {
            tw.WriteLine(TextBox1.Text);
        }
    }
}

实际上,您不必检查文件是否存在,因为StreamWriter将为您执行此操作

using (var tw = new StreamWriter(path, true))
{
    tw.WriteLine(TextBox1.Text);
}
公共流作者( 字符串路径, 布尔附加 )

确定是否将数据附加到文件。如果文件存在且append为false,则会覆盖该文件。如果文件存在且append为true,则将数据追加到文件中。否则,将创建一个新文件


如果文件存在,则可以将其覆盖或附加到。如果文件不存在,此构造函数将创建一个新文件。因此,您不需要检查文件是否存在

您需要确保文件已关闭,然后再进行修改。

如您所见,当文件不存在时,将实际在指定路径上创建一个文件,因此检查它是无用的

我建议删除创建文件的部分,只需开始编写:

public void writeTxt()
{
    string path = @"C:\Users\Morris\Desktop\test.txt";
    using (var tw = new StreamWriter(path, true))
    {
        tw.WriteLine(TextBox1.Text);
        tw.Close();
    }
}

但是,如果您确实想事先创建文件,请记住使用
file.create
调用创建的
FileStream
对象。call将自动为您调用
Flush
Close
,这样就足够安全了,您可以通过以下多种方式执行此操作:

InitializeComponent();
string path = @"C:\Users\Morris\Desktop\test.txt";
if (!File.Exists(path))
{
    using ( File.Create(path) ) ; // This will generate warnings that you're not using the object and so on, but that's okay,
}
InitializeComponent();
string path = @"C:\Users\Morris\Desktop\test.txt";
if (!File.Exists(path))
{
    FileStream fs = File.Create(path);
    fs.Dispose();
}
或者像这样:

InitializeComponent();
string path = @"C:\Users\Morris\Desktop\test.txt";
if (!File.Exists(path))
{
    using ( File.Create(path) ) ; // This will generate warnings that you're not using the object and so on, but that's okay,
}
InitializeComponent();
string path = @"C:\Users\Morris\Desktop\test.txt";
if (!File.Exists(path))
{
    FileStream fs = File.Create(path);
    fs.Dispose();
}

尝试更改
文件。创建(路径)使用(File.Create(path))
谢谢,它现在工作正常;)我没有看到另一个问题,很抱歉问了一个类似的问题。这就是为什么你应该将该问题标记为重复问题。
tw
在到达
Close
时被处理。…
当退出corse的作用域时,using
正在关闭
tw
!谢谢你指出这一点。那样的话就不用做了,关了,好的,谢谢你。我正在从头开始修改代码,所以仍然有很多不相关的代码需要删除。但是我不知道StreamWriter会帮我检查它。是的,
否则,会创建一个新文件。
完美我有StramWriter,但我不确定它是否会创建或写入,如果已经存在,似乎我不需要控制它!很好,谢谢!