如果检测到别名未设置,我的应用程序在运行时需要设置 SQL 别名。现在我让它生成一个临时 Reg 文件并通过 regedit.exe 运行它,但是因为我的应用程序是 32 位的(它必须是因为我正在与一些 32 位 dll 互操作,我无法获得 64 位版本)windows当我运行 regedit 到版本 %windir%\SysWow64\regedit.exe
而不是 %windir%\regedit.exe
时正在进行重定向。
这导致我尝试写入 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo]
的 key 被重定向到 32 位子文件夹,并且我显式写入 32 位子文件夹,[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo]
我不知道它们要去哪里。
通常要解决此问题,您只需使用 %windir%\sysnative\xxxx.exe
但 sysnative
重定向到 System32 文件夹而不是根 Windows 文件夹,这是regedit 所在的位置。
有没有一种方法可以在不编写自定义程序来提升和自己完成的情况下解决这个问题?
这是我当前的代码,它失败了。
static void CreateAliases()
{
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
{
using (var key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo"))
{
CheckKeys(key);
}
}
try
{
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo"))
{
CheckKeys(key);
}
}
}
catch
{
//Catch failues if it is 32 bit only.
}
}
private static void CheckKeys(RegistryKey key)
{
//check to see if the key exists.
if (key == null)
{
AddKeys();
return;
}
var value = key.GetValue(@"wi\sql2008");
if (value == null || value.ToString() != String.Concat("DBMSSOCN,wi,", Properties.Settings.Default.wi_sql2008Port))
{
AddKeys();
return;
}
value = key.GetValue(@"wi\sql2005");
if (value == null || value.ToString() != String.Concat("DBMSSOCN,wi,", Properties.Settings.Default.wi_sql2005Port))
{
AddKeys();
return;
}
}
static private void AddKeys()
{
string file = System.IO.Path.GetTempFileName();
using(StreamWriter sw = new StreamWriter(file))
{
sw.WriteLine("Windows Registry Editor Version 5.00");
sw.WriteLine();
sw.WriteLine(@"[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo]");
sw.WriteLine(String.Concat("\"wi\\\\sql2005\"=\"DBMSSOCN,wi,", Properties.Settings.Default.wi_sql2005Port,'"'));
sw.WriteLine(String.Concat("\"wi\\\\sql2008\"=\"DBMSSOCN,wi,", Properties.Settings.Default.wi_sql2008Port,'"'));
sw.WriteLine();
sw.WriteLine(@"[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo]");
sw.WriteLine(String.Concat("\"wi\\\\sql2005\"=\"DBMSSOCN,wi,", Properties.Settings.Default.wi_sql2005Port, '"'));
sw.WriteLine(String.Concat("\"wi\\\\sql2008\"=\"DBMSSOCN,wi,", Properties.Settings.Default.wi_sql2008Port, '"'));
}
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
bool IsAdmin = principal.IsInRole("BUILTIN\\Administrators");
string regedit;
if (Environment.Is64BitProcess)
{
regedit = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "regedit");
}
else
{
regedit = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "sysnative", "regedit"); //regedit.exe does not exist in sysnative.
}
if (IsAdmin)
{
var proc = Process.Start(new ProcessStartInfo(regedit, String.Concat("/s ", file)));
proc.WaitForExit();
}
else
{
MessageBox.Show("Updating registry keys for WI alias, this must be run as administrator");
var proc = Process.Start(new ProcessStartInfo(regedit, String.Concat("/s ", file)) { Verb = "runas", UseShellExecute = true });
proc.WaitForExit();
}
File.Delete(file);
}
这是正在生成的临时文件。
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo]
"wi\\sql2005"="DBMSSOCN,wi,49224"
"wi\\sql2008"="DBMSSOCN,wi,49681"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo]
"wi\\sql2005"="DBMSSOCN,wi,49224"
"wi\\sql2008"="DBMSSOCN,wi,49681"
最佳答案
我会考虑使用 SMO 创建服务器别名 ServerAlias class相反,您不必自己处理注册表访问。
关于c# - 从 32 位程序启动 64 位版本的 regedit 以创建 sql 别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8387596/