首页 > 解决方案 > 使用 OpenFileDialog C# WPF 将 PDF 复制到其他文件夹

问题描述

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

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 选择文件时,程序会自动崩溃。我只需要复制文件,并将复制文件的路径保存在数据库中。

谢谢你的帮助!

标签: c#wpfcopy

解决方案


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

另外,删除多余的逗号f = new FileInfo(dlg.FileName,);


推荐阅读