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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM