简体   繁体   中英

how can i write a byte array to a file using a SaveFileDialog?

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


     }

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM