1、在工程中添加openFileDialog控件:
2、修改一下控件對象的名稱,讓人感覺好像調用系統函數一樣!
3、在OpenFile這個按鈕的響應函數中示例一下:
private: System::Void btn_openFile_Click(System::Object^ sender, System::EventArgs^ e) {
openFileDialog->Filter = "Text Files(*.txt) | *.txt";
openFileDialog->DefaultExt = "*.txt";
openFileDialog->InitialDirectory = System::Windows::Forms::Application::StartupPath;//起始路徑
openFileDialog->FileName = "hi.txt"; //默認打開文件名稱
openFileDialog->RestoreDirectory = true;
openFileDialog->Title = "打開一個txt文件:";
if (openFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK){
String ^strFilePath = openFileDialog->FileNames->GetValue(0)->ToString();//得到當前選中文件的絕對路徑
String ^strFileName = System::IO::Path::GetFileName(openFileDialog->FileName); //獲取當前選中文件名稱
ShowMsg(strFilePath + " " + strFileName);
}
else
{
ShowMsg("Cancled.");
}
}
備註:效果截圖:
---- The End.