简体   繁体   English

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

[英]Copy PDF to other folder with OpenFileDialog C# WPF

I tried complete my program, with this code: 我尝试使用以下代码完成程序:

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

        }

The problem is when select the file with OpenFileDialog the program Crash automatically. 问题是,当使用OpenFileDialog选择文件时,程序会自动崩溃。 I need copy the files only, and save the path of the copy file in the DB. 我只需要复制文件,然后将复制文件的路径保存在数据库中。

Thank you for you help! 谢谢你的帮助!

Change bool? result = dlg.ShowDialog(); bool? result = dlg.ShowDialog(); bool? result = dlg.ShowDialog(); to if (dlg.ShowDialog() == DialogResult.OK) if (dlg.ShowDialog() == DialogResult.OK)

Also, remove the extra comma from f = new FileInfo(dlg.FileName,); 另外,从f = new FileInfo(dlg.FileName,);删除多余的逗号f = new FileInfo(dlg.FileName,);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM