2

Basically I have have a program that creates an array of bytes (manually entered via a richtextbox and I want to be able to create a new file and save the bytes in that file via a SaveFileDialog() method.

The code I have come up with is:

byte[] bytes = Encoding.ASCII.GetBytes(richTextBox1.Text);
Stream stream = new MemoryStream(bytes);

SaveFileDialog file = new SaveFileDialog();
file.ShowDialog();

     if (file.FileName != "")
     {
         using (BinaryWriter bw = new BinaryWriter(stream)) 
         {
             bw.Write(bytes); 
         }


     }
1
  • I think Tittle is Of-Topic please Edit !! PS : SaveFileDiallg() is not a Method. Commented Sep 28, 2011 at 8:43

3 Answers 3

7

You say you've got the bytes "manually entered via a richtextbox" - but you're just getting the ASCII-encoded value of the text. If you were expecting that to (say) parse hex, then you'll be disappointed. It's not really clear what you're trying to do, but if you are trying to save text, you don't need to convert it into a byte array yourself.

Next, you're currently writing to a MemoryStream, so it's clearly not going to save to a file... if you really wanted to do this, you should use a FileStream instead (either constructed directly or via File.OpenWrite etc). However, you don't need to do all that work yourself...

The simplest way to save a bunch of bytes is:

File.WriteAllBytes(file.FileName, bytes);

The simplest way to save a string is:

File.WriteAllText(file.FileName, text); // Optionally specify an encoding too
Sign up to request clarification or add additional context in comments.

3 Comments

I assume that he wants to write in a file exactly bytes, not text. However I don't know
essentially the idea of this is a binary editor. the bytes are going to be entered in the richtextbox and then written to a file. the Encoding.ASCII.GetBytes() method seems to be ok at getting byte values from the richtextbox. i just gotta save it. trying out your solutions now.
@MeinLuck: If it's a binary editor, then you certainly shouldn't be using ASCII.GetBytes - what happens if you're dealing with non-ascii data? You probably want to have a hex representation instead...
1

you can do that simply by using File.WriteAllText method:

    SaveFileDialog file = new SaveFileDialog();
    file.ShowDialog();

    if (file.FileName != "")
    {
        File.WriteAllText(file.FileName, richTextBox1.Text);
    }

1 Comment

What I want to do though is not write ASCII valuest but rather bytes that are initially entered as ascii values in a richtextbox. Thats why i (try to) convert those values to a byte array.
0

You should use FileInfo to Read/Write to files. So you can do more checks before accessing it. Create the ByteArray as you already done it, so you can decide your encoding, and your are safe for the future. Check the response of the user by using the DialogResult, and then crosscheck the result FileName. Please don't forget, that maybe the File already exists, and the user wants to override or append it :-)

        SaveFileDialog file = new SaveFileDialog();
        DialogResult dialogResult = file.ShowDialog();
        if (dialogResult == DialogResult.OK) {
            if (String.IsNullOrEmpty(file.FileName)) {
                //Inform the user
            }
            string path = file.FileName;
            FileInfo fi = new FileInfo(path);

            // Open the stream for writing.
            using (FileStream fs = fi.OpenWrite()) {
                Byte[] info = Encoding.ASCII.GetBytes(richTextBox1.Text);

                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }


        } else {
            //Inform the user
        }

More Information about FileInfo: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

More Information about the OpenWrite Method: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.openwrite.aspx

Comments

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.