0

I have this application where I use windowsForm and UserControl to draw some diagrams. After I am done I want to save them or I want to open an existing file that I created before and keep working on the diagram. So, I want to use the save and open dialog File to save or open my diagrams.


EDIT:

this is what i have :

    //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;
    }

and now I want to do this but it's not working

    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);
            }

        }
    }

but if I try without the fileDialog and i just use

ObjectToFile(model, "d:\\objects.txt");

this works. And I want to save it where I want and with my own name.

1
  • Make it so. Next time try asking a question. Commented Feb 27, 2011 at 18:28

1 Answer 1

1

Check out the SaveFileDialog and OpenFileDialog classes. They are pretty similar, and can be used like this:

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

    ObjectToFile(sfd.FileName);
}

The mechanics of actually saving your file are, obviously, outside the scope of this answer.

Edit: I've updated my answer to reflect the new information in your post.

Sign up to request clarification or add additional context in comments.

3 Comments

i don't understand what i have to do next. sfd is a saveFileDialog. How i put what I save in this sfd? and how i save? please help me
I am working under the assumption that you know how to save/load your file, as I stated in the answer. Since I don't know anything about your data, I can't really help you any further (and, it would probably warrant a separate question anyway)
Based on your new information, I've updated my answer. Your problem is that you were opening the file, but not closing it afterwards. The best way to handle this is to just try saving it, you don't need to open it first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.