WPF - 使用Microsoft.Win32.OpenFileDialog打开文件,使用Microsoft.Win32.SaveFileDialog将文件另存

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;
            }

 

 
posted @ 2014-06-30 16:34  太古月石  阅读(6497)  评论(1编辑  收藏  举报