【问题标题】:Save file with data in created program在创建的程序中保存带有数据的文件
【发布时间】:2015-04-03 10:10:06
【问题描述】:

我用自己的扩展名.cpd制作了一个程序。当我安装并运行它时,它工作得很好,唯一的问题是保存数据。我可以通过双击或从我的程序打开文件,但是当我保存时,它保持空白。我使用工具条进行保存/打开。

我使用了这个代码:

private void SaveProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Cpd File|*.cpd";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
                using (StreamWriter sw = new StreamWriter(s))
                {
                    sw.Write(textbox1.Text);
                    sw.Write(combobox1.Text);
                    sw.Close();
                    sw.Dispose();
                    s.Close();
                    s.Dispose();
                }
            }
        }

这里有什么问题?为什么它不接受我的输入?

【问题讨论】:

  • 写完后使用sw.close();sw.Dispose();s.close();s.Dispose();
  • sw.Write(*****); 行中放置断点并检查:执行它?
  • 是的。它说 Text="200" 。 200 是我的输入
  • 请使用 CloseDispose 方法显示更改的代码
  • 你这样说......它不能放在其他任何地方因为它会超出范围,不会存在..

标签: c# save user-input windows-forms-designer savefiledialog


【解决方案1】:

编辑: 老兄,您需要在第二次使用时关闭并处理流。

 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
                {
                   using (StreamWriter sw = new StreamWriter(s))
                   {
                       sw.Write(textbox1.Text);
                       sw.Write(combobox1.Text);
                       sw.Close();
                       sw.Dispose();
                   }
                   s.Close();
                   s.Dispose();
                }
            }

【讨论】:

  • 坏主意。是否可以写入二进制数据。我们必须找到问题的根源!
  • 我不明白你的意思:是否可以写入二进制数据。你能解释一下吗?
  • in ante stipe Stream 不写入文件。需要处理这个问题!可能将来他(她)会在那里写BinaryData。
  • 如果你想将它与流一起使用,请看这里:string text = textbox1.Text+combobox1.Text; System.IO.StreamWriter 文件 = new System.IO.StreamWriter(@"C:\YourDest\YourFile.cpd"); file.WriteLine(文本);文件。关闭();你满意吗?尝试更简单的事情。
  • 事实上你写的和ante stipe一样。但在她(他)身上,它不起作用。
猜你喜欢
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 2017-11-18
相关资源
最近更新 更多