如何使用c#更改注册表中的SharedSection ?

本文关键字:SharedSection 注册表 何使用 | 更新日期: 2023-09-27 18:09:58

关于我的注册表System''CurrentControlSet''Control''Session Manager''SubSystems中的这个stackoverflow条目,我必须改变值windows>字符串参数SharedSection=SharedSection=1024,20480,7687682048的第三个值。

通过c#实现这一点的最佳方法是什么?

我试了如下:

var myKey = Registry.LocalMachine.OpenSubKey("System''CurrentControlSet''Control''Session Manager''SubSystems").GetValue("Windows");

局部变量myKey包含如下字符串:

"C:''Windows''system32''csrss.exe ObjectDirectory=''Windows SharedSection=1024,20480,768 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=32"

我需要改变值7682048使用正则表达式还是有更好的方法?

如何使用c#更改注册表中的SharedSection ?

例如:

try {
    updateSharedSection(-1, -1, 2048);
}
catch(Exception e) {
    //..
}
  • 第一个参数系统范围堆的最大大小
  • 第二个参数每个桌面堆的大小
  • 第三个参数:与非交互式Windows工作站相关联的桌面堆的大小。

public void updateSharedSection(int z)
{
    updateSharedSection(-1, -1, z);
}
public void updateSharedSection(int x, int y, int z)
{
    RegistryKey key = Registry.LocalMachine.OpenSubKey("System''CurrentControlSet''Control''Session Manager''SubSystems", true);
    key.SetValue("Windows", _sharedSection(x, y, z, key.GetValue("Windows").ToString()));
}
/// <param name="x">the maximum size of the system-wide heap (in kilobytes) / -1 by default</param>
/// <param name="y">the size of each desktop heap / -1 by default</param>
/// <param name="z"> the size of the desktop heap that is associated with a non-interactive Windows station / -1 by default</param>
/// <param name="raw">raw data line</param>
/// <returns></returns>
private string _sharedSection(int x, int y, int z, string raw)
{
    Func<int, string, string> setVal = delegate(int xyz, string def) {
        return (xyz == -1) ? def : xyz.ToString();
    };
    return Regex.Replace(raw, @"SharedSection=('d+),('d+),('d+)", delegate(Match m)
    {
        return string.Format(
            "SharedSection={0},{1},{2}", 
                setVal(x, m.Groups[1].Value),
                setVal(y, m.Groups[2].Value),
                setVal(z, m.Groups[3].Value));
    }, 
    RegexOptions.IgnoreCase);
}