Copy MemoryStream to FileStream and save the file?

You need to reset the position of the stream before copying. outStream.Position = 0; outStream.CopyTo(fileStream); You used the outStream when saving the file using the imageFactory. That function populated the outStream. While populating the outStream the position is set to the end of the populated area. That is so that when you keep on writing … Read more

Edit a specific Line of a Text File in C#

the easiest way is : static void lineChanger(string newText, string fileName, int line_to_edit) { string[] arrLine = File.ReadAllLines(fileName); arrLine[line_to_edit – 1] = newText; File.WriteAllLines(fileName, arrLine); } usage : lineChanger(“new content for this line” , “sample.text” , 34);

Encode a FileStream to base64 with c#

An easy one as an extension method public static class Extensions { public static Stream ConvertToBase64(this Stream stream) { byte[] bytes; using (var memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); bytes = memoryStream.ToArray(); } string base64 = Convert.ToBase64String(bytes); return new MemoryStream(Encoding.UTF8.GetBytes(base64)); } }

Access to the path denied error in C#

You are trying to create a FileStream object for a directory (folder). Specify a file name (e.g. @”D:\test.txt”) and the error will go away. By the way, I would suggest that you use the StreamWriter constructor that takes an Encoding as its second parameter, because otherwise you might be in for an unpleasant surprise when … Read more

What is a stream in C++?

The term stream is an abstraction of a construct that allows you to send or receive an unknown number of bytes. The metaphor is a stream of water. You take the data as it comes, or send it as needed. Contrast this to an array, for example, which has a fixed, known length. Examples where … Read more

FileStream and creating folders

Use Directory.CreateDirectory: Directory.CreateDirectory Method (String) Creates all directories and subdirectories as specified by path. Example: string fileName = @”C:\Users\SomeUser\My Documents\Foo\Bar\Baz\text1.txt”; Directory.CreateDirectory(Path.GetDirectoryName(fileName)); using (FileStream fs = new FileStream(fileName, FileMode.Create)) { // … } (Path.GetDirectoryName returns the directory part of the file name.)