c# - 如何创建文本文件,并用文本框或变量命名

标签 c# file creation

大家好,我是 C# 的新手,对此深表歉意。

我正在尝试创建一个文本文件来保存客户详细信息。但我想用姓氏命名文本文件。即 myfile.txt。

我确定我很接近,但是在将 surname.text 更改为变量然后将其作为我创建的新文件的名称时遗漏了一些东西。

我在问题区域周围放置了 2 **。

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        if ((tb_firstname.Text == "") || (tb_surname.Text == "") || (tb_postcode.Text == ""))
        {
            MessageBox.Show("Missing values from textboxes!");
        }
        else if (
               ****string myfile.txt = (tb_surname.Text);
               File.Exists("myfile.txt"));****
        {
                if (MessageBox.Show("Warning: file already exists. " + 
                                    "Contents will be replaced - " + 
                                     "do you want to continue?", "File Demo",
                                     MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                     MessageBoxDefaultButton.Button1,
                                     MessageBoxOptions.DefaultDesktopOnly) == 
                    DialogResult.Yes)
                {
                    //write lines of text to file
                    StreamWriter outputStream = File.CreateText(myfile.txt);
                    outputStream.WriteLine(tb_firstname.Text);
                    outputStream.WriteLine(tb_surname.Text);
                    outputStream.WriteLine(tb_surname.Text);
                    outputStream.Close();
                    MessageBox.Show("Text written to file successfully!");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Action cancelled existing file contents not replaced!");
                    this.Close();
                }
            }
            else
            {
                //write lines of text to file
                StreamWriter outputStream = File.CreateText("myFile.txt");
                outputStream.WriteLine(tb_firstname.Text);
                outputStream.WriteLine(tb_surname.Text);
                outputStream.WriteLine(tb_postcode.Text);
                outputStream.Close();
                MessageBox.Show("Text written to file successfully!");
                this.Close();
            }
        }
        catch (Exception problem)
        {
            MessageBox.Show("Program has caused an error - " + problem.ToString());
        }
}

任何帮助都会很棒!

最佳答案

你每次都在创建一个名为 myfile.txt 的文件

StreamWriter outputStream = File.CreateText("myFile.txt");

这是您正在使用的字符串文字。

你有这条线:

string myfile.txt = (tb_surname.Text)

它将文本框的内容读入名为 myfile.txt 的变量中。然后您需要在文件创建代码中使用那个:

StreamWriter outputStream = File.CreateText(myFile.txt);

请注意没有引号。

如果文件已经存在,这将覆盖该文件 - 如果您想要追加,您将需要使用以下方法:

StreamWriter outputStream = File.AppendText(myFile.txt);

关于c# - 如何创建文本文件,并用文本框或变量命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13520157/

相关文章:

c# - C# 的 WebP 库

c# - ASP.NET MVC 4 用户身份验证

windows - 在不使用 FindFirstFile 的情况下迭代目录中的文件

python - Python 处理二进制文件有危险吗?

c++ - 2 条语句用正则 new 表达式创建对象,有什么区别吗?

c# - 如何在 asp.net 中增加 pdf 文件的 ASPxGridView 内容大小?

java - 使用流读取文本文件并保存到 BigInteger 数组

java - 我们初始化我们的主对象,并在其构造函数中创建一个新对象。当我们销毁主要对象时,它的创建会发生什么?

c# - 从表中选择较大的 ID