create file and save to it using memorystream - c#

How can i create a file and write to it using the memory stream?
I need to use the memorystream to prevent other threads from trying to access the file.
The data i'm trying to save to a file is html.
How can this be done?

(Presuming you mean how to copy a file's content to a memory stream)
If you are using framework 4:
var memoryStream = new MemoryStream();
using var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
fileStream.CopyTo(memoryStream);

Here are code to create file
byte[] data = System.Text.Encoding.ASCII.GetBytes("This is a sample string");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(data, 0, data.Length);
ms.Close();

Related

Writing a stream of content to file in c#

I have a variable which holds a stream of content .
The variable is speicified as shown below ,
Task<Stream> tempStream = ....
Specifically this tempStream variable holds a A pdf stream.
I want to to write this stream into a file . How can I do this in c# ?
the following should work:
path = #"Path\\to\\output";
using (FileStream outputFileStream = new FileStream(path, FileMode.Create))
{
tempStream.CopyTo(outpoutFileStream);
}

Writing to MemoryStream not working as expected

I'm using the DryWetMidi library to process some MIDI data.
First I get the MIDI Data as a MemoryStream from the Clipboard:
MemoryStream ms = (MemoryStream)Clipboard.GetDataObject().GetData("Standard MIDI File");
MidiFile mid = MidiFile.Read(ms);
Then I do some stuff with the midi:
mid.RemoveNotes(n => n.NoteName == NoteName.FSharp);
Now I want to write it back to the Clipboard. I managed to do this like this:
using (FileStream file = new FileStream("file.mid", FileMode.Create, FileAccess.
{
mid.Write(file);
}
using (MemoryStream ms2 = new MemoryStream())
using (FileStream file = new FileStream("file.mid", FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms2.Write(bytes, 0, (int)file.Length);
Clipboard.Clear();
Clipboard.SetData(midiFormat, ms2);
}
File.Delete("file.mid");
As you can see, first I write the MIDI to a file, then I read that file into a MemoryStream which I then write into the Clipboard. This makes not much sense, because it would be simpler to write it to a MemoryStream directly. Also, I don't want to write a file to the users file system. But there's the problem. I tried it like this:
using (MemoryStream ms2 = new MemoryStream())
{
mid.Write(ms2);
}
This doesn't give me an error, but the MemoryStream is completely empty. Calling ms2.Length results in a System.ObjectDisposedException.
How can I write the midi directly into the MemoryStream?
EDIT: Here's the link to the DryWetMidi Write() Method.
Second Edit: Here's a piece of code that won't work:
MemoryStream ms = (MemoryStream)Clipboard.GetDataObject().GetData(midiFormat);
MidiFile mid = MidiFile.Read(ms);
mid.RemoveNotes(n => n.NoteName == NoteName.FSharp);
MemoryStream ms2 = new MemoryStream();
mid.Write(ms2);
var T = ms2.Length; //This will throw an exception
Third Edit: I am 100% sure that the code posted is exactly the same I'm running. Here's the StackTrace. (Gist because formatting was terrible on SO).
As far as I can see, DryWetMidi uses BinaryWriter to write to stream. And the default behaviour of BinaryWriter is that when it is disposed, It'll dispose the stream as well.
You can't read from MemoryStream when it's disposed but you can call ToArray().
byte[] result;
using (MemoryStream ms2 = new MemoryStream())
{
mid.Write(ms2);
result = ms2.ToArray();
}

Reading gif file from base64 into picturebox any other way? C#

I Have gif image in base64.
Currently I am approaching this way. reading base64 gif file and writing it to byte array and writing it back to image file to disk and reading from the file to picturebox.image.
byte[] imageBytes = Convert.FromBase64String(body);
//* this is write file to disk and read
string filename = Username;
File.WriteAllBytes(filename, imageBytes);
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
pictureBox1.Image = Image.FromStream(fs);
Now, I want to write it to memory without writing it to disk file. like in a form of variable image. that can be assigned to picturebox. Is there any possible of this. because I have to do repeatedly many times for many images.
So I would like find a different approach without writing saving file to disk and reading it again.
Any help appreciated.
byte[] imageBytes = Convert.FromBase64String(body);
MemoryStream stream = new MemoryStream(imageBytes);
pictureBox1.Image = Image.FromStream(stream);
byte[] imageBytes = Convert.FromBase64String(body);
using (var ms = new MemoryStream(imageBytes))
{
pictureBox1.Image = Image.FromStream(ms);
}
Be aware the MemoryStream class is IDisposable so you should Dispose() it. This can happen with using or with try/catch/finally block.

How to use a file loaded in memory stream or memory mapped files?

I am having issues understanding the proper way I am supposed to use files loaded in memory streams or memory mapped files in conjunction to methods that only accept string file paths.
Let's say I am writing a file to memory stream like this:
using (MemoryStream ms = new MemoryStream())
using (FileStream file = new FileStream("path_to_my_external_file.jpg", FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);
}
How would I be able to use the memory stream then with a function that only accepts a string file path as a parameter like GenericFunction (string filePath)?

unable to save dynamically created MemoryStream with rebex sftp

I'm using StreamWriter to generate a dynamic file and holding it in a MemoryStream. Everything appears to be alright until I go to save the file using rebex sftp.
The example they give on their site works fine:
// upload a text using a MemoryStream
string message = "Hello from Rebex FTP for .NET!";
byte[] data = System.Text.Encoding.Default.GetBytes(message);
System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
client.PutFile(ms, "message.txt");
However the code below does not:
using (var stream = new MemoryStream())
{
using (var writer = new StreamWriter(stream))
{
writer.AutoFlush = true;
writer.Write("test");
}
client.PutFile(stream, "test.txt");
}
The file "test.txt" is saved, however it is empty. Do I need to do more than just enable AutoFlush for this to work?
After writing to the MemoryStream, the stream is positioned at the end. The PutFile method reads from the current position to the end. That's exactly 0 bytes.
You need to position the stream at the beginning before passing it to PutFile:
...
}
stream.Seek(0, SeekOrigin.Begin);
client.PutFile(stream, "test.txt");
You may also need to prevent the StreamWriter from disposing the MemoryStream:
var writer = new StreamWriter(stream);
writer.Write("test");
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
client.PutFile(stream, "test.txt");

Categories