{"id":83,"date":"2016-02-12T13:10:16","date_gmt":"2016-02-12T13:10:16","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=83"},"modified":"2022-02-17T06:24:15","modified_gmt":"2022-02-17T06:24:15","slug":"save-file-as","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/file-input-and-output\/save-file-as","title":{"rendered":"Save File As"},"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\t\/\/----- NEW FILE DIALOG -----\r\n\tconst string MY_FILE_EXTENSION = &quot;txt&quot;;\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(ApMain.LastFileDirectory)))\r\n\t\tApMain.LastFileDirectory = 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;       \/\/&quot;txt files (*.txt)|*.txt|All files (*.*)|*.*&quot;\r\n\tSelectFileDialog.FilterIndex = 1;              \/\/(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 = ApMain.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)     \/\/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\tApMain.LastFileDirectory = SaveAsFilename.Substring(0, (SaveAsFilename.LastIndexOf(&quot;\\\\&quot;) + 1));\r\n\t\telse\r\n\t\t\tApMain.LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\r\n\r\n\t\t\/\/......DO IT...\r\n\t}\r\n<\/code><\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System.IO; Save As Dialog Box \/\/&#8212;&#8211; NEW FILE DIALOG &#8212;&#8211; const string MY_FILE_EXTENSION = &quot;txt&quot;; String SaveAsFilename; \/\/If last directory is not valid then default to My Documents if (!Directory.Exists(Path.GetDirectoryName(ApMain.LastFileDirectory))) ApMain.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; [&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-83","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\/83","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=83"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/83\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}