Winform下判断文件和文件夹是否存在

时间:2023-03-09 04:59:30
Winform下判断文件和文件夹是否存在
  1. //选择文件夹
  2. FolderBrowserDialog dia = new FolderBrowserDialog();
  3. if (dia.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  4. {
  5. string filePath = dia.SelectedPath;
  6. Directory.Exists(filePath);//判断文件夹是否存在
  7. }
  8. //选择文件
  9. OpenFileDialog dia = new OpenFileDialog();
  10. if (dia.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  11. {
  12. string filePath = dia.SelectedPath;
  13. File.Exists(filePath);//判断文件是否存在
  14. }