What's new

C# Help?

E

Evor

Enthusiast
Messages
237
Reaction score
17
C#

If i have)

private void openProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("notepad++");
}

it will open Notepad++

but if notepad++ is not installed the how would i say (program not found)
 
S

SecurityEd

Enthusiast
Messages
187
Reaction score
59
1). You can check if it's been installed(via registry) *requires more code
Code:
public static bool IsApplictionInstalled(string p_name)
{
    string displayName;
    RegistryKey key;

    // search in: CurrentUser
    key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (String keyName in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(keyName);
        displayName = subkey.GetValue("DisplayName") as string;
        if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
        {
            return true;
        }
    }

    // search in: LocalMachine_32
    key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (String keyName in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(keyName);
        displayName = subkey.GetValue("DisplayName") as string;
        if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
        {
            return true;
        }
    }

    // search in: LocalMachine_64
    key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (String keyName in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(keyName);
        displayName = subkey.GetValue("DisplayName") as string;
        if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
        {
            return true;
        }
    }

    // NOT FOUND
    return false;
}
2). Or you can check if the file exists *requires less ccode
Code:
if(File.Exists(@"C:\Program Files (x86)\Notepad++\notepad++.exe") == true)
{
   //exists
}
else
{
   //nope
}
 
Last edited:
S

S7 Pro

Seasoned Member
Messages
2,511
Reaction score
1,603
C#

If i have)

private void openProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("notepad++");
}

it will open Notepad++

but if notepad++ is not installed the how would i say (program not found)
Well if you want, you could do something like:

Code:
private void openProgramToolStripMenuItem_click(object sender, EventArgs e) {
    try {
        Process.Start("notepad++");
    } catch (Win32Exception ex) {    // thrown if there was an error opening the file, i.e. file DNE
        // there was an error opening the program above
    }
}

This was just from reading https://msdn.microsoft.com/en-us/library/e8zac0ca(v=vs.110).aspx, I'd also look at handling the other errors properly as well.
 
Top Bottom