{"id":222,"date":"2016-12-23T12:11:03","date_gmt":"2016-12-23T12:11:03","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=222"},"modified":"2022-02-17T06:24:14","modified_gmt":"2022-02-17T06:24:14","slug":"memorystream-general","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/memory\/memorystream\/memorystream-general","title":{"rendered":".MemoryStream General"},"content":{"rendered":"<h4>\n\tRead file into Memory Stream<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tMemoryStream SourceXmlMemoryStream = new MemoryStream(File.ReadAllBytes(filename));\r\n<\/code><\/pre>\n<h4>\n\tWrite File From Memory Stream<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tFileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write);\t\t\/\/Overwrite the existing file\r\n\tOutputXmlMemoryStream.WriteTo(file);\r\n\tfile.Close();\r\n\tOutputXmlMemoryStream.Close();\r\n<\/code><\/pre>\n<h4>\n\tSeek<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tSourceXmlMemoryStream.Seek(0, SeekOrigin.Begin);\r\n<\/code>\r\n<\/pre>\n<p>\n\tor\n<\/p>\n<pre>\r\n<code>\r\n\tSourceXmlMemoryStream.Position = 0\r\n<\/code><\/pre>\n<h4>\n\tClose Memory Stream<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tSourceXmlMemoryStream.Dispose();\r\n<\/code><\/pre>\n<h4>\n\tWrite MemoryStream To File<br \/>\n<\/h4>\n<p>\n\t(Overwrite if alreads exists)\n<\/p>\n<pre>\r\n<code>\r\n\tMemoryStream1.Position = 0;\r\n\tSystem.IO.File.WriteAllBytes(&quot;C:\\\\_Downloaded\\\\test1.bin&quot;, MemoryStream1.ToArray());\r\n<\/code><\/pre>\n<h4>\n\tConvert Memory Stream to String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tstring MyString = Encoding.UTF8.GetString(MyMemoryStream.ToArray());\r\n<\/code><\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Read file into Memory Stream MemoryStream SourceXmlMemoryStream = new MemoryStream(File.ReadAllBytes(filename)); Write File From Memory Stream FileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write); \/\/Overwrite the existing file OutputXmlMemoryStream.WriteTo(file); file.Close(); OutputXmlMemoryStream.Close(); Seek SourceXmlMemoryStream.Seek(0, SeekOrigin.Begin); or SourceXmlMemoryStream.Position = 0 Close Memory Stream SourceXmlMemoryStream.Dispose(); Write MemoryStream To File (Overwrite if alreads exists) MemoryStream1.Position = 0; System.IO.File.WriteAllBytes(&quot;C:\\\\_Downloaded\\\\test1.bin&quot;, MemoryStream1.ToArray()); Convert Memory [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-memorystream"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":273,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/222\/revisions\/273"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}