Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

c# - Check if the directory exists and if it does not exist show a message box stating that it does not exist

in windows forms 2017 Express, i want to check if the directory the file exists and if it does not exist show a messagebox stating that the file it does not exist

Code:

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

And what i want is, if a directory does not exist, then Put a

Messagebox.Show("Directory Not Found);

Just like this: If this directory: "Y:TesterMAIN.exe" dont exist put that messagebox.show.

I want to check if the directory exists, and if exists, check if the file exists too; Or just if the directory exists or not and return a messagebox.

question from:https://stackoverflow.com/questions/65832273/check-if-the-directory-exists-and-if-it-does-not-exist-show-a-message-box-statin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
/* 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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...