c# - 使用 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 选择文件时,程序会自动崩溃。我只需要复制文件,并将复制文件的路径保存在数据库中。
谢谢你的帮助!
解决方案
更改bool? result = dlg.ShowDialog();
为if (dlg.ShowDialog() == DialogResult.OK)
另外,删除多余的逗号f = new FileInfo(dlg.FileName,);
推荐阅读
- wcf - 使用 WCF 服务自动向客户端发送值
- python - Pandas - 恢复许多热编码(虚拟变量)
- java - 当我按下屏幕上的“Jugar”按钮时,我的应用程序关闭
- android - Firebase 身份验证库不存在
- python - 如何从基类调用方法
- python - 我想重置索引,但想拥有数据框中的所有列
- excel - 使用宏进行数据验证
- mysql - 使用 mysql 和 knex 将 SELECT COUNT 保存到变量中
- azure - Azure 事件中心定期向数据存储生成 508 B 消息
- function - 在 TypeScript 中定义返回函数的函数类型时出现“找不到参数”