使用OpenFileDialog C#WPF将PDF复制到其他文件夹

时间:2018-06-11 17:34:59

标签: c# wpf copy

我尝试使用以下代码完成我的程序:

private void btnBuscar_Click(object sender, RoutedEventArgs e)
        {
            FileInfo f = null;
            OpenFileDialog dlg;
            dlg = new OpenFileDialog();
            dlg.Filter = "Document Files (*.doc;*pdf)|*.doc;*pdf";
            dlg.InitialDirectory = Environment.SpecialFolder.UserProfile.ToString();
            dlg.Title = "Seleccione su archivo de cotización.";
            //Open the Pop-Up Window to select the file
            bool? result = dlg.ShowDialog();
            if (result == true)
            {
                f = new FileInfo(dlg.FileName,);
                using (Stream s = dlg.OpenFile())
                {
                    TextReader reader = new StreamReader(s);
                    string st = reader.ReadToEnd();
                    txtPath.Text = dlg.FileName;
                }
                File.Copy(dlg.FileName,@"C:\Formatos\m1");
            }

        }

问题是当用OpenFileDialog选择文件程序时会自动崩溃。我只需要复制文件,并将复制文件的路径保存在DB中。

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

bool? result = dlg.ShowDialog();更改为if (dlg.ShowDialog() == DialogResult.OK)

另外,请从f = new FileInfo(dlg.FileName,);

中删除额外的逗号