{"id":220,"date":"2010-04-29T09:09:56","date_gmt":"2010-04-29T09:09:56","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=220"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"user-open-file-dialog","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/file-input-output-serialization\/user-open-file-dialog","title":{"rendered":"Open File Dialog"},"content":{"rendered":"<pre><code>\r\nusing namespace System::IO;\r\n<\/code><\/pre>\n<h4>Open File Dialog Example<\/h4>\n<pre><code>\r\n\tString ^LastFileDirectory;\r\n\tString ^Filename;\r\n\r\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)\r\n\tif (!Directory::Exists(LastFileDirectory))\r\n\t\tLastFileDirectory = Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments);\r\n\r\n\t\/\/----- FILE OPEN DIALOG BOX -----\r\n\tOpenFileDialog ^SelectFileDialog = gcnew OpenFileDialog();\r\n\r\n\tSelectFileDialog->Filter = \"All Files (*.*)|*.*\";\t\t\/\/\"txt files (*.txt)|*.txt|All files (*.*)|*.*\"\r\n\tSelectFileDialog->FilterIndex = 1;\t\t\t\t\/\/(First entry is 1, not 0)\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)\r\n\t{\r\n\t\t\/\/----- OPEN THE FILE -----\r\n\t\tFilename = SelectFileDialog->FileName;\r\n\r\n\t\t\/\/STORE LAST USED DIRECTORY\r\n\t\tif (Filename->LastIndexOf(\"\\\\\") >= 0)\r\n\t\t\tLastFileDirectory = Filename->Substring(0, (Filename->LastIndexOf(\"\\\\\") + 1));\r\n<\/code><\/pre>\n<h4>To Select Multiple Files To Open<\/h4>\n<pre><code>\r\n\t\/\/As part of the dialog setup\r\n\tSelectFileDialog->Multiselect = true;\r\n\t\/\/To get the selected filenames\r\n\tFilename = SelectFileDialog->FileName;\t\t\t\t\t\/\/Will contain the first file\r\n\tarray<String^> ^Filenames = SelectFileDialog->FileNames;\t\t\/\/Will contain all files\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using namespace System::IO; Open File Dialog Example String ^LastFileDirectory; String ^Filename; \/\/If last directory is not valid then default to My Documents (if you don&#8217;t include this the catch below won&#8217;t occur for null strings so the start directory is undefined) if (!Directory::Exists(LastFileDirectory)) LastFileDirectory = Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments); \/\/&#8212;&#8211; FILE OPEN DIALOG BOX &#8212;&#8211; OpenFileDialog ^SelectFileDialog = [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[34,33],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-file-input-output-serialization","tag-ge-t","tag-lastfiledirectory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":1123,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/220\/revisions\/1123"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}