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 aFileStreaminstead (either constructed directly or viaFile.OpenWriteetc). However, you don’t need to do all that work yourself…The simplest way to save a bunch of bytes is:
The simplest way to save a string is: