1. WPF 使用這個方法打開文件,很方便,而且可以記住上次打開的路徑。
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog(); openFileDialog.Multiselect = false; openFileDialog.Filter = "AllFiles|*.*"; if ((bool)openFileDialog.ShowDialog()) { txtPath.Text = openFileDialog.FileName; }
txtPath 是界面上的控件名稱
想要知道打開文件的歷史列表:http://stackoverflow.com/questions/11144770/how-does-wpf-openfiledialog-track-directory-of-last-opened-file
這個方法應該Work,等需要的時候再深入了。
2. 當需要將文件另存為的時候,也用這種Win32的函數。用起來也挺方便的。
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = "信息"; // Default file name dlg.DefaultExt = ".xlsx"; // Default file extension dlg.Filter = "Excel 工作薄 (.xlsx)|*.xlsx"; // Filter files by extension // Show save file dialog box Nullable<bool> result = dlg.ShowDialog(); string filename = string.Empty; // Process save file dialog box results if (result == true) { // Save document filename = dlg.FileName; } else { return; }
