简体   繁体   English

将多个表单输入保存到文件

[英]Saving multiple pieces of form input to a file

What would be the best way to save a windows form to a file with a few text boxes collecting user input. 将Windows表单保存到带有几个收集用户输入的文本框的文件的最佳方法是什么。 I using this at the moment: 我现在使用这个:

if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog1.FileName, tB1.Text);
                File.WriteAllText(saveFileDialog1.FileName, tB2.Text);
            }

This is fine for saving the input from the first text box but when it comes to the other it wont save the data entered. 这对于保存来自第一个文本框的输入很好,但是涉及另一个文本框时,它不会保存输入的数据。

how about concatenating the two textboxes? 如何串联两个文本框? for clarity, 为了清楚起见,

string forSaving = tB1.Text + "\n" + tB2.Text;
File.WriteAllText(saveFileDialog1.FileName, forSaving);

or 要么

File.WriteAllText(saveFileDialog1.FileName, tB1.Text + "\n" + tB2.Text);

UPDATE 1 更新1

string firstName = "FirstName: " + txtFirstName.Text;
string lastName = "LastName: " + txtLastName.Text;
string personAddress = "FirstName: " + txtAddress.Text;
string details = firstName + "\n" + lastName + "\n" + personAddress;
File.WriteAllText(saveFileDialog1.FileName, tB1.Text + "\n" + details);

然后连接这两个texbox;

File.WriteAllText(saveFileDialog1.FileName, tB1.Text + Environment.NewLine + tB2.Text );

The best way would probably be to create a method in your form that will return a string with all of the values from the TextBoxes into whatever format you want. 最好的方法可能是在您的窗体中创建一个方法,该方法将以文本框的形式返回一个包含所有值的字符串,并将其转换为所需的格式。 Something like this would work: 这样的事情会起作用:

File.WriteAllText(saveFileDialog1.fileName, OutputUserInfo());

Then inside OutputUserInfo() you can have whatever formatting to the data that you want so you can understand what they put in. 然后,在OutputUserInfo()您可以对所需的数据进行任何格式化,以便了解它们的内容。

Edit Example of OutputUserInfo() 编辑OutputUserInfo()示例

private string OutputUserInfo() {
    return  "First Name: " + tbFirstName.Text + Environment.NewLine +
            "Surname: " + tbSurname.Text + Environment.NewLine +
            "Address" + tbAddress.Text + Environment.NewLine;
            // Just keep adding whatever you want on here.
            // Add the descriptions if you want, it will probably help
}

You could also have this in different formats (CSV, or whatever). 您也可以使用其他格式(CSV或其他格式)。 But if you're just doing a plain text file, this could be easiest. 但是,如果您只是做一个纯文本文件,这可能是最简单的。 It is up to you though. 不过,这取决于您。

File.WriteAllText is probably bad because it overwrites your content. File.WriteAllText可能不好,因为它会覆盖您的内容。

Creates a new file, writes the specified string to the file, and then closes the file. 创建一个新文件,将指定的字符串写入该文件,然后关闭该文件。 If the target file already exists, it is overwritten. 如果目标文件已经存在,则将其覆盖。

Instead go with File.AppendAllText which 取而代之的是File.AppendAllText

Appends the specified stringto the file, creating the file if it does not already exist. 将指定的字符串附加到文件,如果文件不存在,则创建该文件。

If it was me I would use the StreamWriter / StreamReader Classes since they have WriteLine and Readline methods respectively. 如果是我,我将使用StreamWriter / StreamReader类,因为它们分别具有WriteLineReadline方法。

ie something like this 即像这样的东西

private void button1_Click(object sender, EventArgs e)
{
    if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
        {
            sw.WriteLine(tB1.Text);
            sw.WriteLine(tB2.Text);
            sw.WriteLine(tB3.Text);
            sw.WriteLine(tB4.Text);
            sw.Close();
        }

   }

}

private void button2_Click(object sender, EventArgs e)
{
    if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
        {
            tB1.Text = sr.ReadLine();
            tB2.Text = sr.ReadLine();
            tB3.Text = sr.ReadLine();
            tB4.Text = sr.ReadLine();
            sr.Close();
        }
    }

}

There we go, use Encoding to Append all the string. 到这里,使用Encoding附加所有字符串。

    private void button1_Click(object sender, EventArgs e)
    {
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string line = string.Format("{0},{1}"
            , textBox1.Text
            , textBox2.Text);
            File.AppendAllText(saveFileDialog1.FileName, line, Encoding.GetEncoding(1252));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM