将列表框的项目保存到文本文件

发布于 2024-08-22 07:01:18 字数 139 浏览 1 评论 0原文

如何使用 SaveFileDialoglistbox 项目的内容保存到文本文件?

我还想向文本文件添加其他信息,并添加一个 MessageBox 说明成功时已保存。

How can I save the contents of my listbox items to a text file using a SaveFileDialog?

I also want to add additional information to the text file and also add a MessageBox saying saved when it's been successful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白日梦 2024-08-29 07:01:18
        var saveFile = new SaveFileDialog();
        saveFile.Filter = "Text (*.txt)|*.txt";
        if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            using (var sw = new StreamWriter(saveFile.FileName, false))
                foreach (var item in listBox1.Items)
                    sw.Write(item.ToString() + Environment.NewLine);
            MessageBox.Show("Success");
        }

另请注意 StreamWriter 的类型为 编码

        var saveFile = new SaveFileDialog();
        saveFile.Filter = "Text (*.txt)|*.txt";
        if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            using (var sw = new StreamWriter(saveFile.FileName, false))
                foreach (var item in listBox1.Items)
                    sw.Write(item.ToString() + Environment.NewLine);
            MessageBox.Show("Success");
        }

Also take note the StreamWriter has a Type of Encoding.

醉生梦死 2024-08-29 07:01:18

这应该可以做到。

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog f = new OpenFileDialog();

    f.ShowDialog();                 

    ListBox l = new ListBox();
    l.Items.Add("one");
    l.Items.Add("two");
    l.Items.Add("three");
    l.Items.Add("four");

    string textout = "";

    // assume the li is a string - will fail if not
    foreach (string li in l.Items)
    {
        textout = textout + li + Environment.NewLine;
    }

    textout = "extra stuff at the top" + Environment.NewLine + textout + "extra stuff at the bottom";
    File.WriteAllText(f.FileName, textout);

    MessageBox.Show("all saved!");
}

this should do it.

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog f = new OpenFileDialog();

    f.ShowDialog();                 

    ListBox l = new ListBox();
    l.Items.Add("one");
    l.Items.Add("two");
    l.Items.Add("three");
    l.Items.Add("four");

    string textout = "";

    // assume the li is a string - will fail if not
    foreach (string li in l.Items)
    {
        textout = textout + li + Environment.NewLine;
    }

    textout = "extra stuff at the top" + Environment.NewLine + textout + "extra stuff at the bottom";
    File.WriteAllText(f.FileName, textout);

    MessageBox.Show("all saved!");
}
南笙 2024-08-29 07:01:18

SaveFileDialogShowDialog() 一起使用以将其显示给用户,如果成功,则使用其 OpenFile() 获取 ( File)Stream 您写入的内容。 msdn 页面上有一个示例。

ListBox 可以通过其 Items 属性进行访问,该属性只是其上项目的集合。

A SaveFileDialog is used with ShowDialog() to show it to the user, and if it's successful, using its OpenFile() to get the (File)Stream that you write to. There's an example on the msdn page.

A ListBox can be accessed through its Items property, which is simply a collection of the items on it.

め七分饶幸 2024-08-29 07:01:18

保存

   // fetch the selected Text from your list
   string textToRight = listBox1.SelectedItem.ToString();  

   // Write to a file       
   StreamWriter sr = File.CreateText(@"testfile.txt");       
   sr.Write(textToRight);
   sr.Close();

消息

   // display Message
   MessageBox.Show( "Information Saved Successfully" ); 

To Save

   // fetch the selected Text from your list
   string textToRight = listBox1.SelectedItem.ToString();  

   // Write to a file       
   StreamWriter sr = File.CreateText(@"testfile.txt");       
   sr.Write(textToRight);
   sr.Close();

Message

   // display Message
   MessageBox.Show( "Information Saved Successfully" ); 
温馨耳语 2024-08-29 07:01:18

那里发生了一些事情 - 确保将它们分开,例如

  • 获取列表框内容
  • 附加信息
  • 写入文件

请注意!!保存文件时可能会出现无数的异常,查看文档并以某种方式处理它们......

// Get list box contents
var sb = new StringBuilder();
foreach (var item in lstBox.Items)
{
    // i am using the .ToString here, you may do more
    sb.AppendLine(item);
}
string data = sb.ToString();

// Append Info
data = data + ????....

// Write File
void Save(string data)
{
    using(SaveFileDialog saveFileDialog = new SaveFileDialog())
    {
        // optional
        saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);

        //saveFileDialog.Filter = ???;

        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            File.WriteAllText(saveFileDialog.Filename);
            MessageBox.Show("ok", "all good etc");
        }
        else
        {
        // not good......
        }
    }
}

You have a few things going on there - make sure you split them up, e.g.

  • Get list box contents
  • Append Info
  • Write File

Please note!! There is a myriad of exceptions you can get while saving a file, see the docs and handle them somehow...

// Get list box contents
var sb = new StringBuilder();
foreach (var item in lstBox.Items)
{
    // i am using the .ToString here, you may do more
    sb.AppendLine(item);
}
string data = sb.ToString();

// Append Info
data = data + ????....

// Write File
void Save(string data)
{
    using(SaveFileDialog saveFileDialog = new SaveFileDialog())
    {
        // optional
        saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);

        //saveFileDialog.Filter = ???;

        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            File.WriteAllText(saveFileDialog.Filename);
            MessageBox.Show("ok", "all good etc");
        }
        else
        {
        // not good......
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文