简体   繁体   中英

What is the common way to throw exception if my exe file that i need to start does not exist on the machine

in my application i am start capinfos.exe that is part of Wireshark. in the constructor i am check if Wireshark install on the machine:

private string _filePath = "";

public Capinfos(string capturePath)
{
    if (Directory.Exists(@"C:\Program Files (x86)\Wireshark"))
    {
        _capInfos = @"C:\Program Files (x86)\Wireshark\capinfos.exe";
    }
    else if (Directory.Exists(@"C:\Program Files\Wireshark"))
    {
        _capInfos = @"C:\Program Files\Wireshark\capinfos.exe";
    }

    _filePath = capturePath;
}

what is the best way to do it and throw an exception if the file does not exist on the machine: please install Wireshark

private string _filePath = "";

public Capinfos(string capturePath) throws FileNotFoundException
{
    if (Directory.Exists(@"C:\Program Files (x86)\Wireshark"))
    {
        _capInfos = @"C:\Program Files (x86)\Wireshark\capinfos.exe";
    }
    else if (Directory.Exists(@"C:\Program Files\Wireshark"))
    {
        _capInfos = @"C:\Program Files\Wireshark\capinfos.exe";
    } else
    {
       throw new FileNotFoundException(@"Wireshark installation not found");
    } 

    _filePath = capturePath;
}

You can then catch the exception by using this code:

   try
    {
        Capinfos("path");
    }
    catch (FileNotFoundException ex)
    {
        Messagebox.Show("Please install wireshark.");
    }

I don't have C# installed, this was written by hand. Hope it's fine! Here's an excellent resource to learn on exceptions: http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx

就像是:

throw new FileNotFoundException("Could not find " + _capInfos, _capInfos);

Not sure if this is what you want, but try to use a try-catch block. You could attempt to start the .exe in the try block and if it fails, throw a FileNotFoundException and create a popup box in the catch block that will alert the user of what they need to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM