1

我有一个带有一些文本框和组合框等的表单,以及来自用户的自定义女仆信息。所以我想要做的就是将框中的信息保存到一个 .txt 文件中,您可以像普通的 .doc 一样阅读后记。但是有一些我不知道如何处理的问题。首先,保存.txt 文件的路径应该是用户的costumniceabel。换句话说,用户应该能够选择保存 .txt 文件的位置。完成后,我想拥有它,以便它在该文本文档中写下所有内容。我现在很重要,所以我理解你是否无法阅读它,但请尝试询问你是否不清楚。

4

1 回答 1

0

我会这样做:

按钮回调:

创建一个 Button 并在其回调中插入以下代码:

private void button1_Click(object sender, EventArgs e)
{
    // Here comes the treatment
}

内部处理:

构建一个可解析的字符串(每个属性一行)。

String str = "";
str += prop1 + "=" + this.textBox1.Text + "\n";
...

使用 aSaveFileDialog保存文件。

// Set the default file name
String path = getPreviousUserPath();    // A function to write by yourself
savefile.InitialDirectory = Path.GetDirectoryName(path);
savefile.FileName = Path.GetFileName(path);
// Set filters - this can be done in properties as well
savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*|"
// Display the save dialog
if(savefile.ShowDialog() == DialogResult.OK)
{
    // Save the properties to the file selected by the user
    using(StreamWriter sw = new StreamWriter(savefile.FileName))
    {
        sw.WriteLine(str);
    }
    setPreviousUserPath(savefile.FileName);    // A function to write by yourself
}

注意:之后,您将能够加载文件并使用 '=' 和 '\n' 分隔符对其进行解析,以取回先前保存的属性。

注意:我只是在这里给出标题...

于 2013-06-15T01:25:44.733 回答