.NET 6.0 x86应用程序未成功设置注册表键。

huangapple go评论63阅读模式
英文:

.NET 6.0 x86 app doesn't set registry key successful

问题

我们想要通过一个.NET 6.0应用程序设置OnlyUseLatestCLR注册表键。该应用程序将在x86和x64机器上运行,这些机器没有安装.NET 6。因此,我们将这个应用程序发布为单文件并使用x86架构,使用以下命令:

dotnet publish -a x86 --sc true

以下是我们用来设置OnlyUseLatestCLR值的代码:

RegistryKey clrKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\.NETFramework", true);
clrKey.SetValue("OnlyUseLatestCLR", 0);

但是以上代码不会将OnlyUseLatestCLR值设置为0。没有抛出任何异常。

我们尝试过的:

  1. 在Visual Studio中调试应用程序,它可以成功设置该键。
  2. 使用x64架构发布它,在x64机器上可以成功设置OnlyUseLatestCLR。但是该应用程序无法在x86机器上启动。
  3. 使用命令REG ADD "HKLM\Software\Microsoft\.NETFramework" /t REG_DWORD /v OnlyUseLatestCLR /d 0 /f使用批处理文件设置该值。如果我们手动调用批处理文件,它可以成功设置,但如果我们从.NET 6.0应用程序以x86单文件发布时调用它,设置失败,没有异常。
  4. 尝试使用.reg文件导入注册表项。如果我们手动导入.reg文件,它可以成功。但是如果我们尝试使用.NET 6.0应用程序以x86单文件发布时以编程方式导入,它会再次失败,没有任何异常。
    Process proc = new Process();
    proc.StartInfo.FileName = "regedit.exe";
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.UseShellExecute = false;
    string command = "/s regeidfile.reg";
    proc.Start();
    proc.WaitForExit();

预期:
我们希望.NET 6应用程序可以以单文件发布,并成功设置OnlyLatestCLR。

英文:

We want to set the OnlyUseLatestCLR registry key with a .NET 6.0 app. This app will run on both x86 and x64 machines and the machines won't have .NET 6 installed. So we publish this app as single file and with x86 arch use this command:

dotnet publish -a x86 --sc true

And following code that we are using to set the OnlyUseLatestCLR value:

RegistryKey clrKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\.NETFramework", true);
clrKey.SetValue("OnlyUseLatestCLR", 0);

But above code doesn't set the OnlyUseLatestCLR value to 0. There is no exception thrown.

What we tried:

  1. Debug the application in Visual Studio, it can set the key successully.
  2. Publish it with x64 arch, it can set OnlyUseLatestCLR successful on x64 machine. But the application cannot be launch on x86 machine.
  3. Use batch file with command REG ADD "HKLM\Software\Microsoft\.NETFramework" /t REG_DWORD /v OnlyUseLatestCLR /d 0 /f to set the value. If we call the batch file manually, it can set successful, but if we call it from .NET 6.0 app with publishing as x86 single file, set failed without exception.
  4. Try import the key with .reg file. If we import the .reg manually, it can successful. But if we try import with .NET 6.0 app programmatically and publishing as x86 single file, it failed without any exception again.
    Process proc = new Process();
    proc.StartInfo.FileName = "regedit.exe";
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.UseShellExecute = false;
    string command = "/s regeidfile.reg";
    proc.Start();
    proc.WaitForExit();

Expected:
We want the .NET 6 app can be published with single file and set the OnlyLatestCLR successfully.

答案1

得分: 1

我已通过以下代码和项目属性配置进行了解决:

  1. 在打开注册表键时,使用以下代码分配注册表视图:
RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey frameworkKey = baseKey.OpenSubKey(@"Software\Microsoft\.NETFramework", true);
  1. 为项目添加以下属性:
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<PlatformTarget>x86</PlatformTarget>
  1. 然后使用以下命令发布应用程序:
dotnet publish -a x86 --sc true
英文:

I have resolved it with following changes in code and configurations in project properties.

  1. When opening the registry key, assign the registry view with following code:

    <!-- language: lang-cs -->

    RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    RegistryKey frameworkKey = baseKey.OpenSubKey(@&quot;Software\Microsoft\.NETFramework&quot;, true);
    
  2. Add following properties for project:

    &lt;RuntimeIdentifier&gt;win-x86&lt;/RuntimeIdentifier&gt;
    &lt;PlatformTarget&gt;x86&lt;/PlatformTarget&gt;
    
  3. Then publish the app with command:

    dotnet publish -a x86 --sc true
    

huangapple
  • 本文由 发表于 2023年5月29日 14:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定