• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

[C#] Prüfen ob Programm installiert

Excelsion

Neues Mitglied
Hallo,
mein Programm soll direkt beim Aufruf prüfen, ob ein bestimmtes Programm installiert ist.

Mein Code:

Code:
[COLOR=#0000ff][B]// Beim Laden wird meine Function "IsInstalled();" aufgerufen und ausgewertet ...
private[/B][/COLOR] [COLOR=#ff0000]void[/COLOR] [COLOR=#191970][B]Form1_Load[/B][/COLOR][COLOR=#006400]([/COLOR][COLOR=#ff0000]object[/COLOR] sender[COLOR=#006400],[/COLOR] EventArgs e[COLOR=#006400])[/COLOR]
        [COLOR=#006400]{[/COLOR]
            [COLOR=#ff0000]string[/COLOR] ProgramName = [COLOR=#0000ff]"ProgrammName"[/COLOR][COLOR=#006400];[/COLOR]
            [COLOR=#ff0000][B]bool[/B][/COLOR] ergebnis = [COLOR=#191970][B]IsInstalled[/B][/COLOR][COLOR=#006400]([/COLOR]ProgramName[COLOR=#006400]);[/COLOR]
            [COLOR=#ff0000]string[/COLOR] ergebnis1 = Convert[COLOR=#006400].[/COLOR][COLOR=#191970][B]ToString[/B][/COLOR][COLOR=#006400]([/COLOR]ergebnis[COLOR=#006400]);[/COLOR]
            label6[COLOR=#006400].[/COLOR]Text = ergebnis1[COLOR=#006400];[/COLOR]

            [COLOR=#0000ff][B]if[/B][/COLOR] [COLOR=#006400]([/COLOR]ergebnis == [COLOR=#008b8b][B]true[/B][/COLOR][COLOR=#006400])[/COLOR]
            [COLOR=#006400]{[/COLOR]
                label6[COLOR=#006400].[/COLOR]Text = [COLOR=#0000ff]"Installiert!"[/COLOR][COLOR=#006400];[/COLOR]
                button1[COLOR=#006400].[/COLOR]Enabled = [COLOR=#008b8b][B]true[/B][/COLOR][COLOR=#006400];[/COLOR]
                label6[COLOR=#006400].[/COLOR]ForeColor = Color[COLOR=#006400].[/COLOR]Green[COLOR=#006400];[/COLOR]
            [COLOR=#006400]}[/COLOR]
            [COLOR=#0000ff][B]else[/B][/COLOR]
            [COLOR=#006400]{[/COLOR]
                label6[COLOR=#006400].[/COLOR]Text = [COLOR=#0000ff]"Nicht installiert!"[/COLOR][COLOR=#006400];[/COLOR]
            [COLOR=#006400]}[/COLOR]
        [COLOR=#006400]}[/COLOR]
        
 // Jetzt die Funktion um zu überprüfen ob Programm installiert ist ...

 [COLOR=#0000ff][B]public[/B][/COLOR] [COLOR=#ff0000][B]bool[/B][/COLOR] [COLOR=#191970][B]IsInstalled[/B][/COLOR][COLOR=#006400]([/COLOR][COLOR=#ff0000]string[/COLOR] ProgramName[COLOR=#006400])[/COLOR]
        [COLOR=#006400]{[/COLOR]
            [COLOR=#0000ff][B]if[/B][/COLOR] [COLOR=#006400]([/COLOR]ProgramName [COLOR=#006400]![/COLOR]= [COLOR=#0000ff]""[/COLOR][COLOR=#006400])[/COLOR]
            [COLOR=#006400]{[/COLOR]
                [COLOR=#ff0000]string[/COLOR] strUninstallList = [COLOR=#0000ff]@"[/COLOR][COLOR=#0000ff]SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"[/COLOR][COLOR=#006400];[/COLOR]
                [COLOR=#ff0000]object[/COLOR] obj[COLOR=#006400];[/COLOR]
                RegistryKey RegKeyUninstallList = Registry[COLOR=#006400].[/COLOR]LocalMachine[COLOR=#006400];[/COLOR]
                RegistryKey SubKeyUninstallList = Registry[COLOR=#006400].[/COLOR]LocalMachine[COLOR=#006400];[/COLOR]

                [COLOR=#0000ff][B]foreach[/B][/COLOR] [COLOR=#006400]([/COLOR][COLOR=#ff0000]string[/COLOR] strSubKey [COLOR=#0000ff][B]in[/B][/COLOR] RegKeyUninstallList[COLOR=#006400].[/COLOR][COLOR=#191970][B]OpenSubKey[/B][/COLOR][COLOR=#006400]([/COLOR]strUninstallList[COLOR=#006400]).[/COLOR][COLOR=#191970][B]GetSubKeyNames[/B][/COLOR][COLOR=#006400]())[/COLOR]
                [COLOR=#006400]{[/COLOR]
                    obj = SubKeyUninstallList[COLOR=#006400].[/COLOR][COLOR=#191970][B]OpenSubKey[/B][/COLOR][COLOR=#006400]([/COLOR]strUninstallList [COLOR=#006400]+[/COLOR] [COLOR=#0000ff]"\\"[/COLOR] [COLOR=#006400]+[/COLOR] strSubKey[COLOR=#006400]).[/COLOR][COLOR=#191970][B]GetValue[/B][/COLOR][COLOR=#006400]([/COLOR][COLOR=#0000ff]"DisplayName"[/COLOR][COLOR=#006400]);[/COLOR]

                    [COLOR=#0000ff][B]if[/B][/COLOR] [COLOR=#006400]([/COLOR]obj [COLOR=#006400]![/COLOR]= [B]null[/B][COLOR=#006400])[/COLOR]
                    [COLOR=#006400]{[/COLOR]
                        [COLOR=#0000ff][B]if[/B][/COLOR] [COLOR=#006400]([/COLOR]obj[COLOR=#006400].[/COLOR][COLOR=#191970][B]ToString[/B][/COLOR][COLOR=#006400]().[/COLOR][COLOR=#191970][B]ToLower[/B][/COLOR][COLOR=#006400]().[/COLOR][COLOR=#191970][B]Contains[/B][/COLOR][COLOR=#006400]([/COLOR]ProgramName[COLOR=#006400].[/COLOR][COLOR=#191970][B]ToLower[/B][/COLOR][COLOR=#006400]()));[/COLOR]
                            
                    [COLOR=#006400]}[/COLOR]

                [COLOR=#006400]}[/COLOR]
            [COLOR=#006400]}[/COLOR]

            [COLOR=#000080]return[/COLOR] [COLOR=#008b8b][B]false[/B][/COLOR][COLOR=#006400];[/COLOR]
        [COLOR=#006400]}[/COLOR]
Leider sagt er mir immer, dass das Programm nicht installiert ist...

Könnt ihr mir helfen? :)
 
Werbung:
Habe mich zwar noch nie mit C# befasst, aber ich ich sehe in deiner Methode "IsInstalled()" nirgendwo ein "return true", somit gibt diese Methode ja grundsätzlich nur "false" zurück.

Ich denke mal dieser Abschnitt hier:
Code:
if (obj != null)
{
	if (obj.ToString().ToLower().Contains(ProgramName.ToLower()));
}

Sollte so aussehen:
Code:
if (obj != null)
{
	if (obj.ToString().ToLower().Contains(ProgramName.ToLower()))
	{
		return true;
	}
}
 
Zurück
Oben