{"id":106,"date":"2016-02-17T14:26:17","date_gmt":"2016-02-17T14:26:17","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=106"},"modified":"2024-06-23T10:19:50","modified_gmt":"2024-06-23T09:19:50","slug":"open-file-dialog","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/file-input-and-output\/open-file-dialog","title":{"rendered":"Open File Dialog"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\nusing System.IO;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Open File Dialog Example<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tString Filename;\n\n\t\/\/If last directory is not valid then default to My Documents (if you don't include this the catch below won't occur for null strings so the start directory is undefined)\n\tif (!Directory.Exists(ApMain.LastFileDirectory))\n\t\tApMain.LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\n\n\t\/\/----- FILE OPEN DIALOG BOX -----\n\tOpenFileDialog SelectFileDialog = new OpenFileDialog();\n\n\tSelectFileDialog.Filter = \"All Files (*.*)|*.*\";\t\t\/\/\"txt files (*.txt)|*.txt|All files (*.*)|*.*\"\n\tSelectFileDialog.FilterIndex = 1;\t\t\t\t\/\/(First entry is 1, not 0)\n\ttry\n\t{\n\t\tSelectFileDialog.InitialDirectory = ApMain.LastFileDirectory;\n\t}\n\tcatch (Exception)\n\t{\n\t\tSelectFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\n\t}\n\t\/\/Display dialog box\n\tif (SelectFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)\n\t{\n\t\t\/\/----- OPEN THE FILE -----\n\t\tFilePath = SelectFileDialog.FileName;\n\n\t\t\/\/STORE LAST USED DIRECTORY\n\t\tif (FilePath.LastIndexOf(\"\\\\\") >= 0)\n\t\t{\n\t\t\tApMain.LastFileDirectory = FilePath.Substring(0, (FilePath.LastIndexOf(\"\\\\\") + 1));\n\n\t\t\t\/\/FileName = FilePath.Substring(FilePath.LastIndexOf(\"\\\\\") + 1);\t\t\/\/&lt;&lt;If you want just the filename\n\t\t\/\/if (FileName.LastIndexOf(\".\") >= 0)\n\t\t\/\/\tFileName = FileName.Substring(0, (FileName.LastIndexOf(\".\") + 0));\t\t\/\/&lt;&lt;&lt;If you want the file extension removed\n\t\t}\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Open File Dialog Example<\/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-106","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\/106","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=106"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":1342,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/106\/revisions\/1342"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}