Add a safe method to get a registry value

This commit is contained in:
_aLfa_ 2014-09-25 23:13:01 +02:00
parent 3a6caa443e
commit 656d4b390a
4 changed files with 61 additions and 58 deletions

View File

@ -162,40 +162,18 @@ namespace Nikse.SubtitleEdit.Logic
private static string GetInstallerPath()
{
Microsoft.Win32.RegistryKey key = null;
try
var value = Utilities.GetRegistryValue(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\SubtitleEdit_is1", "InstallLocation");
if (value != null && Directory.Exists(value))
{
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\SubtitleEdit_is1");
if (key != null)
{
var value = (string)key.GetValue("InstallLocation");
if (value != null && Directory.Exists(value))
{
return value;
}
}
return value;
}
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\SubtitleEdit_is1");
if (key != null)
{
var value = (string)key.GetValue("InstallLocation");
if (value != null && Directory.Exists(value))
{
return value;
}
}
}
catch (System.Security.SecurityException)
value = Utilities.GetRegistryValue(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\SubtitleEdit_is1", "InstallLocation");
if (value != null && Directory.Exists(value))
{
// The user does not have the permissions required to read the registry key.
}
finally
{
if (key != null)
{
key.Dispose();
}
return value;
}
return null;
}

View File

@ -3462,5 +3462,40 @@ namespace Nikse.SubtitleEdit.Logic
t.Change(millisecondsDelay, -1);
return tcs.Task;
}
/// <summary>
/// Retrieves the specified registry subkey value.
/// </summary>
/// <param name="keyName">The path of the subkey to open.</param>
/// <param name="valueName">The name of the value to retrieve.</param>
/// <returns>The value of the subkey requested, or <b>null</b> if the operation failed.</returns>
public static string GetRegistryValue(string keyName, string valueName)
{
Microsoft.Win32.RegistryKey key = null;
try
{
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(keyName);
if (key != null)
{
var value = key.GetValue(valueName);
if (value != null)
{
return (string)value;
}
}
}
catch (System.Security.SecurityException)
{
// The user does not have the permissions required to read the registry key.
}
finally
{
if (key != null)
{
key.Dispose();
}
}
return null;
}
}
}

View File

@ -529,26 +529,18 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers
}
// XP via registry path
var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\VideoLAN\VLC");
if (key != null)
{
path = (string)key.GetValue("InstallDir");
if (path != null && Directory.Exists(path))
path = Path.Combine(path, fileName);
if (File.Exists(path))
return path;
}
path = Utilities.GetRegistryValue(@"SOFTWARE\VideoLAN\VLC", "InstallDir");
if (path != null && Directory.Exists(path))
path = Path.Combine(path, fileName);
if (File.Exists(path))
return path;
// Winows 7 via registry path
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\VideoLAN\VLC");
if (key != null)
{
path = (string)key.GetValue("InstallDir");
if (path != null && Directory.Exists(path))
path = Path.Combine(path, fileName);
if (File.Exists(path))
return path;
}
path = Utilities.GetRegistryValue(@"SOFTWARE\Wow6432Node\VideoLAN\VLC", "InstallDir");
if (path != null && Directory.Exists(path))
path = Path.Combine(path, fileName);
if (File.Exists(path))
return path;
path = Path.Combine(@"C:\Program Files (x86)\VideoLAN\VLC", fileName);
if (File.Exists(path))

View File

@ -247,20 +247,19 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers.MpcHC
return path;
}
var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{2ACBF1FA-F5C3-4B19-A774-B22A31F231B9}_is1");
if (key != null)
path = Utilities.GetRegistryValue(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{2ACBF1FA-F5C3-4B19-A774-B22A31F231B9}_is1", "InstallLocation");
if (path != null)
{
path = (string)key.GetValue("InstallLocation");
path = Path.Combine(path, "mpc-hc64.exe");
if (File.Exists(path))
return path;
}
path = Path.Combine(@"C:\Program Files\MPC-HC\mpc-hc64.exe");
path = @"C:\Program Files\MPC-HC\mpc-hc64.exe";
if (File.Exists(path))
return path;
path = Path.Combine(@"C:\Program Files (x86)\MPC-HC\mpc-hc64.exe");
path = @"C:\Program Files (x86)\MPC-HC\mpc-hc64.exe";
if (File.Exists(path))
return path;
}
@ -277,20 +276,19 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers.MpcHC
return path;
}
var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{2624B969-7135-4EB1-B0F6-2D8C397B45F7}_is1");
if (key != null)
path = Utilities.GetRegistryValue(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{2624B969-7135-4EB1-B0F6-2D8C397B45F7}_is1", "InstallLocation");
if (path != null)
{
path = (string)key.GetValue("InstallLocation");
path = Path.Combine(path, "mpc-hc.exe");
if (File.Exists(path))
return path;
}
path = Path.Combine(@"C:\Program Files (x86)\MPC-HC\mpc-hc.exe");
path = @"C:\Program Files (x86)\MPC-HC\mpc-hc.exe";
if (File.Exists(path))
return path;
path = Path.Combine(@"C:\Program Files\MPC-HC\mpc-hc.exe");
path = @"C:\Program Files\MPC-HC\mpc-hc.exe";
if (File.Exists(path))
return path;
}