WPF 打开文件(OpenFileDialog)/保存文件(SaveFileDialog) 选择基本操作

1.打开文件保存 OpenFileDialog

包含过滤文件;获取文件完整路径;允许多选

  private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.Filter = "Sql 文件(*.sql)|*.sql";
            //过滤文件类型
            openFileDialog.Filter = "文档(*.txt)|*.txt|所有文件(*.*)|*.*";

            //允许多选
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == true)
            {
                //FileName 完整路径:相对路径+文件名+后缀
                //文件内容读取
                string name = openFileDialog.FileName;
                string[] names = openFileDialog.FileNames;
            }
        }

2. 保存选择SaveFileDialog 

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "文档(*.txt)|*.txt|所有文件(*.*)|*.*";

            if (saveFileDialog.ShowDialog() == true)
            {
                //文件内容读取
                string name = saveFileDialog.FileName;
            }

        }

你可能感兴趣的:(WPF,wpf)