{"id":175,"date":"2016-03-02T10:38:35","date_gmt":"2016-03-02T10:38:35","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=175"},"modified":"2022-02-17T06:24:14","modified_gmt":"2022-02-17T06:24:14","slug":"write-new-file","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/file-input-and-output\/write-new-file","title":{"rendered":"Write New File"},"content":{"rendered":"<pre>\r\n<code>\r\nusing System.IO;\r\n<\/code><\/pre>\n<h4>\n\tSave As Dialog Box<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tString SaveAsFilename;\r\n\r\n\t\/\/If last directory is not valid then default to My Documents\r\n\tif (!Directory.Exists(Path.GetDirectoryName(LastFileDirectory)))\r\n\t\tLastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\r\n\r\n\t\/\/----- SAVE FILE DIALOG BOX -----\r\n\tSaveFileDialog SelectFileDialog = new SaveFileDialog();\r\n\r\n\tSelectFileDialog.Filter = &quot;Comma Separated Values (*.&quot; + MY_FILE_EXTENSION + &quot;)|*.&quot; + MY_FILE_EXTENSION;\t\t\/\/&quot;txt files (*.txt)|*.txt|All files (*.*)|*.*&quot;\r\n\tSelectFileDialog.FilterIndex = 1;\t\t\t\t\/\/(First = 1, not 0)\r\n\tSelectFileDialog.Title = &quot;Save Live Log As&quot;;\r\n\tSelectFileDialog.OverwritePrompt = true;\r\n\ttry\r\n\t{\r\n\t\tSelectFileDialog.InitialDirectory = LastFileDirectory;\r\n\t}\r\n\tcatch (Exception)\r\n\t{\r\n\t\tSelectFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\r\n\t}\r\n\t\/\/Display dialog box\r\n\tif (SelectFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)\t\t\/\/This ensures correct filename extension is used\r\n\t{\r\n\t\t\/\/----- SAVE THE FILE -----\r\n\t\tSaveAsFilename = SelectFileDialog.FileName;\r\n\r\n\t\t\/\/STORE LAST USED DIRECTORY\r\n\t\tif (SaveAsFilename.LastIndexOf(&quot;\\\\&quot;) &gt;= 0)\r\n\t\t\tLastFileDirectory = SaveAsFilename.Substring(0, (SaveAsFilename.LastIndexOf(&quot;\\\\&quot;) + 1));\r\n\t\telse\r\n\t\t\tLastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\r\n<\/code><\/pre>\n<h4>\n\tWrite StringTo A New File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tStreamWriter StreamWriter1= new StreamWriter(&quot;C:\\\\test.txt&quot;);\r\n\t\/\/StreamWriter StreamWriter1= new StreamWriter(&quot;C:\\\\test.txt&quot;, false, System.Text.Encoding.Unicode);\t\/\/False = overwrite - Alternative method specifying the encoding\r\n\tStreamWriter1.Write(MyString);\r\n\tStreamWriter1.Close();\r\n<\/code><\/pre>\n<h4>\n\tWrite Bytes To A New File<br \/>\n<\/h4>\n<p>\n\t(Write unsigned char or arrays of unsigned char to a file &#8211; not strings or anything else)\n<\/p>\n<pre>\r\n<code>\r\n\tFileStream HeaderOutputFile = new FileStream(MyFilePath, FileMode.Create, FileAccess.Write, FileShare.None);\r\n\r\n\tHeaderOutputFile.WriteByte(0x01);\r\n\t\/\/...\r\n\tHeaderOutputFile.Close();\r\n<\/code><\/pre>\n<h4>\n\tWrite Binary (Inc Strings) To A New File<br \/>\n<\/h4>\n<p>\n\tThe problem with writing using FileStream is that its not string friendly. You can write strings using the StreamWriter, but if you want to write strings and binary data then it&#39;s easier to use the Binary Writer.\n<\/p>\n<pre>\r\n<code>\r\n\tString sTemp;\r\n\r\n\tFileStream MyOutputFile = new FileStream(&quot;C:\\\\test.txt&quot;, FileMode.Create, FileAccess.Write, FileShare.None);\r\n\tBinaryWriter MyOutputWriter = new BinaryWriter(MyOutputFile);\r\n\r\n\tsTemp = &quot;Hello World!\\r\\n&quot;;\r\n\tMyOutputWriter.Write(sTemp.ToCharArray());\t\/\/ToCharArray to avoid String type byte being written at start of the line\r\n\tMyOutputWriter.Write(Convert.ToByte(0x01));\t\/\/Write will write any variable it is given using as many bytes as needed\r\n\t\/\/...\r\n\tMyOutputWriter.Close();\r\n\tMyOutputFile.Close();\r\n\r\n\t\/\/GOOD IDEA TO ADD THIS TO TRY CATCH BLOCK AND REMOVE THE TWO .CLOSE() LINES ABOVE:-\r\n\tfinally\r\n\t{\r\n\t\tif (MyOutputWriter != nullptr)\r\n\t\t\tMyOutputWriter.Close();\r\n\t\tif (MyOutputFile != nullptr)\r\n\t\t\tMyOutputFile.Close();\r\n\t}\r\n<\/code><\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System.IO; Save As Dialog Box String SaveAsFilename; \/\/If last directory is not valid then default to My Documents if (!Directory.Exists(Path.GetDirectoryName(LastFileDirectory))) LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); \/\/&#8212;&#8211; SAVE FILE DIALOG BOX &#8212;&#8211; SaveFileDialog SelectFileDialog = new SaveFileDialog(); SelectFileDialog.Filter = &quot;Comma Separated Values (*.&quot; + MY_FILE_EXTENSION + &quot;)|*.&quot; + MY_FILE_EXTENSION; \/\/&quot;txt files (*.txt)|*.txt|All files (*.*)|*.*&quot; SelectFileDialog.FilterIndex = 1; [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-file-input-and-output"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/175","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=175"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/175\/revisions\/177"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}