如何在winForms中保存和打开文件?

时间:2011-02-27 18:20:46

标签: winforms file-io

我有这个应用程序,我使用windowsForm和UserControl绘制一些图表。完成后我想保存它们或者我想打开我之前创建的现有文件并继续处理图表。所以,我想使用保存和打开对话框文件来保存或打开我的图表。


修改

这就是我所拥有的:

    //save the object to the file

    public bool ObjectToFile(Object model, string FileName)
    {
        try
        {
            System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter  _BinaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            _BinaryFormatter.Serialize(_MemoryStream, model);

            byte[] _ByteArray = _MemoryStream.ToArray();
            System.IO.FileStream _FileStream = new System.IO.FileStream(FileName,         System.IO.FileMode.Create, System.IO.FileAccess.Write);
            _FileStream.Write(_ByteArray.ToArray(), 0, _ByteArray.Length);
            _FileStream.Close();

            _MemoryStream.Close();
            _MemoryStream.Dispose();
            _MemoryStream = null;
            _ByteArray = null; 

            return true;

        }
        catch (Exception _Exception)
        {           
            Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
        }
        return false;

    }

//load the object from the file

    public Object FileToObject(string FileName)
    {
        try
        {
            System.IO.FileStream _FileStream = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
            long _TotalBytes = new System.IO.FileInfo(FileName).Length;
            byte[] _ByteArray = _BinaryReader.ReadBytes((Int32)_TotalBytes);
            _FileStream.Close();
            _FileStream.Dispose();
            _FileStream = null;
            _BinaryReader.Close();

            System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_ByteArray);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            _MemoryStream.Position = 0;
            return _BinaryFormatter.Deserialize(_MemoryStream);

        }
        catch (Exception _Exception)
        {
            Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
        }
        return null;
    }

现在我想这样做,但它不起作用

    public void save()
    {
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if (saveFileDialog1.OpenFile() != null)
            {
                ObjectToFile(model, saveFileDialog1.FileName);
            }

        }
    }

但如果我尝试没有fileDialog而我只是使用

ObjectToFile(model, "d:\\objects.txt");
这是有效的。而且我想用我自己的名字保存它。

1 个答案:

答案 0 :(得分:1)

查看SaveFileDialogOpenFileDialog课程。它们非常相似,可以像这样使用:

using(SaveFileDialog sfd = new SaveFileDialog()) {
    sfd.Filter = "Text Files|*.txt|All Files|*.*";
    if(sfd.ShowDialog() != DialogResult.OK) {
        return;
    }

    ObjectToFile(sfd.FileName);
}

实际上保存文件的机制显然超出了这个答案的范围。

编辑:我已更新我的回答以反映您帖子中的新信息。