检查目录是否存在,如果不存在则显示不存在的消息框



在windows forms 2017 Express中,我想检查文件的目录是否存在,如果它不存在,则显示一个消息框,说明该文件不存在

代码:

string str = @"Y:TesterMAIN.exe";
Process process = new Process();
process.StartInfo.FileName = str;
process.Start();

我想要的是,如果一个目录不存在,然后放一个

Messagebox.Show("Directory Not Found);

就像这样:如果这个目录:"Y:TesterMAIN.exe"put that messagebox.show.

我想检查目录是否存在,如果存在,检查文件是否也存在;或者只判断目录是否存在,并返回一个消息框。

/* Use: 
if (!OSTools.ExeFileHandler.Perform(@"Y:TesterMAIN.exe")
MessageBox.Show("Action failed");  
*/
namespace OSTools
{
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
public static class ExeFileHandler
{
public static bool Perform(string strExe)
{
if (!Directory.Exists(Path.GetDirectoryName(strExe)))
MessageBox.Show("Directory Not Found");
else
if (File.Exists(strExe))
{
Process process = new Process();
process.StartInfo.FileName = strExe;
process.Start();
return true;
}
else MessageBox.Show($"File {strExe} Not Found");
return false;
}
}
}

最新更新