C# WPF 选择文件弹窗与保存文件弹窗

最近需要做一个Excel导入导出的功能场景,导出首先得选择保存路径,导入得选择导入文件,都是基本功能,于是记录一下,方便日后使用。
需要引用:using Microsoft.Win32

1 . 选择文件弹窗,得到选择的文件路径

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择文件";
            openFileDialog.Multiselect = true;
            openFileDialog.Filter = "Excel文件|*.xlsx";
            if (!(bool)openFileDialog.ShowDialog())
            {
                return; //被点了取消
            }
            var filename = openFileDialog.FileName;

2 . 文件保存弹窗 ,得到保存路径

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "窗口标题";
            saveFileDialog.InitialDirectory = App.ExportChartPath;
            saveFileDialog.OverwritePrompt = true;
            saveFileDialog.AddExtension = true;
            saveFileDialog.DefaultExt = "xlsx";
            saveFileDialog.Filter = "Excel文件|*.xlsx";
            saveFileDialog.FileName = "门板价格.xlsx";
            if (!(bool)saveFileDialog.ShowDialog())
            {
                return; //被点了取消
            }
            var filename = saveFileDialog.FileName; //得到保存路径及文件名

你可能感兴趣的:(C#)