Есть ли способ конвертировать этот скрипт powershell в C # - PullRequest
0 голосов
/ 09 мая 2019

Я хочу преобразовать этот код в C #.

Function CheckFileInstalled {

param (
    [string]$pathProg     = "C:\Program Files\WinRAR\WinRAR.exe",
    [string]$nameProg     = "Winrar"
)

$testFileProg = Test-Path $pathProg

$x86 = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
    Where-Object { $_.GetValue( "DisplayName" ) -like "*$nameProg*" } ).Length -gt 0;

$x64 = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |
    Where-Object { $_.GetValue( "DisplayName" ) -like "*$nameProg*" } ).Length -gt 0;


return ( $testFileProg -and ($x86 -or $x64) )

}

if( CheckFileInstalled ) {
    Write-Host "Program installed." 
}
else {
    Write-Host "Failed to install."
}

1 Ответ

2 голосов
/ 09 мая 2019

Попробуйте, но вам могут потребоваться права администратора для реестра

public bool CheckFileInstalled(string pathProg, string nameProg)
{
    bool pathExist = Directory.Exists(pathProg);
    bool x86 = false;
    bool x64 = false;

    RegistryKey x86Key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall", true);
    if(x86Key.GetValueNames().Contains(nameProg)) x86 = true;

    RegistryKey x64Key = Registry.LocalMachine.OpenSubKey(@"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall", true);
    if(x64Key.GetValueNames().Contains(nameProg)) x64 = true;

    return (pathExist && (x86 || x64));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...