我尝试使用以下代码完成我的程序:
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中。
谢谢你的帮助!
答案 0 :(得分:0)
将bool? result = dlg.ShowDialog();
更改为if (dlg.ShowDialog() == DialogResult.OK)
另外,请从f = new FileInfo(dlg.FileName,);