System.IO.FileStream FileAccess vs FileShare

FileAccess says what you are going to do with the file. Pretty easy to understand, you’ll know you are going to read or write. FileShare is the much trickier one since it requires you to step into the shoes of another programmer. It determines what another process can do if it also has the file … Read more

Why does BinaryWriter prepend gibberish to the start of a stream? How do you avoid it?

They are not byte-order marks but a length-prefix, according to MSDN: public virtual void Write(string value); Writes a length-prefixed string to [the] stream And you will need that length-prefix if you ever want to read the string back from that point. See BinaryReader.ReadString(). Additional Since it seems you actually want a File-Header checker Is it … Read more

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

simultaneous read-write a file in C#

Ok, two edits later… This should work. The first time I tried it I think I had forgotten to set FileMode.Append on the oStream. string test = “foo.txt”; var oStream = new FileStream(test, FileMode.Append, FileAccess.Write, FileShare.Read); var iStream = new FileStream(test, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var sw = new System.IO.StreamWriter(oStream); var sr = new System.IO.StreamReader(iStream); var … Read more