我写了这段代码只选择pdf文件,但它不能正常工作
OpenFileDialog fd = new OpenFileDialog();
fd.ShowDialog();
fd.Filter = "Pdf files (*.Pdf)|*.Pdf";
答案 0 :(得分:11)
在打开对话框之前,您需要先设置Filter
。
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "Pdf files (*.Pdf)|*.Pdf"; //this should be before
fd.ShowDialog();
答案 1 :(得分:1)
Habib有正确答案,但我觉得我要补充一点,你应该检查对ShowDialog
的回复,以确保用户没有取消对话框。如果他们在没有选择文件的情况下取消对话框,那么OpenFileDialog
会说文件名是“”,这对你的应用程序的其余部分没用。
示例强>
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "PDF Files(*.pdf)|*.pdf";
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Do stuff here
}
else
{
// The user cancelled the request to select a PDF
}
希望这有帮助