c#清理注册表文件.reg以删除重复的条目

时间:2023-01-03 00:15:33

A company I work for sometimes uses super mandatory profiles for their users. Unfortunately this can cause lots of issues with user settings not being saved at log off and so on. To work around this a .reg file is run at startup. For some of these settings regmon was used to record making the settings change and a reg file created from this. The trouble with this is their are lots of duplicate entries making it larger and larger each time this has been done. By the way I didn't implement this procedure I just have to live with it.

我工作的公司有时会为其用户使用超级强制配置文件。不幸的是,这会导致很多问题,用户设置不会在注销时保存等等。要解决此问题,请在启动时运行.reg文件。对于其中一些设置,regmon用于记录进行设置更改以及从此创建的reg文件。这样做的问题在于它们有很多重复的条目,每次完成它都会越来越大。顺便说一句,我没有实现这个程序,我只需要忍受它。

I am going to attempt to clean this down to it's minimum but am wondering if anyone has done this before. What I am thinking is, I read the lines into an 2d array so:

我将尝试将其清理到最低限度,但我想知道是否有人之前已经这样做了。我在想的是,我把线条读成了一个二维数组,所以:

[HKEY_CURRENT_USER\Software\CELCAT\CT\SAT\Settings]
"OpenTTVS"="0"
"OTTFrmW"="420"
"OTTFrmH"="321"

[HKEY_CURRENT_USER \ Software \ CELCAT \ CT \ SAT \ Settings]“OpenTTVS”=“0”“OTTFrmW”=“420”“OTTFrmH”=“321”

becomes:
[HKEY_CURRENT_USER\Software\CELCAT\CT\SAT\Settings], "OpenTTVS"="0" [HKEY_CURRENT_USER\Software\CELCAT\CT\SAT\Settings], "OTTFrmW"="420" [HKEY_CURRENT_USER\Software\CELCAT\CT\SAT\Settings], "OTTFrmH"="321"

成为:[HKEY_CURRENT_USER \ Software \ CELCAT \ CT \ SAT \ Settings],“OpenTTVS”=“0”[HKEY_CURRENT_USER \ Software \ CELCAT \ CT \ SAT \ Settings],“OTTFrmW”=“420”[HKEY_CURRENT_USER \ Software \ CELCAT \ CT \ SAT \ Settings],“OTTFrmH”=“321”

Then remove duplicates in reverse order so if a setting is changed twice it removes the entries first in the file.

然后以相反的顺序删除重复项,因此如果设置更改两次,则会先删除文件中的条目。

Then sort the array by the hive and output the key every time and the hive when it changes. Sorry if the terminology is wrong.

然后通过配置单元对数组进行排序,并在每次输出密钥时输出密钥。对不起,如果术语有误。

Will this work? Probably better using a collection rather than an array but you get the idea.

这会有用吗?可能更好地使用集合而不是数组,但你明白了。

2 个解决方案

#1


For a first try i would read this .reg file into a tree, just the way regedit looks like. By building up the tree you just check if a node exists. If not, created it else overwrite the existing value.

第一次尝试我会将这个.reg文件读入树中,就像regedit的样子一样。通过构建树,您只需检查节点是否存在。如果没有,则创建它否则覆盖现有值。

So you'll get just the last entry which take into effect. After creating this tree, you'll have to read back the current tree and you're ready.

因此,您将获得生效的最后一个条目。创建此树后,您必须回读当前树,然后就可以了。

While writing this, i'll got another idea: Open the .reg file in a texteditor. Replace "[HKEY_CURRENT_USER\" against "[HKEY_CURRENT_USER\JustSomeKeyThatDoesNotExists" and import this file into the registry. Now open the registry and export the whole "[HKEY_CURRENT_USER\JustSomeKeyThatDoesNotExists" back into a file. Open the file and make the first replacement backwards.

在写这篇文章时,我会有另一个想法:打开texteditor中的.reg文件。将“[HKEY_CURRENT_USER”替换为“[HKEY_CURRENT_USER \ JustSomeKeyThatDoesNotExists”并将此文件导入注册表。现在打开注册表并将整个“[HKEY_CURRENT_USER \ JustSomeKeyThatDoesNotExists”]导回到文件中。打开文件并向后进行第一次更换。

Now you'll have a file with unique value in a sorted order. ;-))

现在,您将按排序顺序拥有一个具有唯一值的文件。 ;-))

#2


You could use a Dictionary of Dictionaries. In pseudo code, you could then do something like this:

您可以使用词典词典。在伪代码中,您可以执行以下操作:

Dictionary<String, Dictionary<String, String>> regSettings
Dictionary<String, String> current
for line in sourcefile {
    if line.startswith("[") {
        if !regSettings.haskey(line) {
            regSettings.add(line, new Dictionary<String, String>() )
        }
        current = regSettings[line]
    } else {
        key, value = line.split("=")
        current[key] = value;
    }
}

There are some details to account for (blank lines, comments, poorly remembered function names that should be adjusted for C#...), but that's the basic idea. When you come across a duplicate setting, the later value will replace the older one in the collection. To write the file, just output the reg header, then iterate your collections, writing the top-level key name, followed by all values for that key.

有一些细节需要考虑(空白行,注释,记忆不佳的函数名称应该为C#调整......),但这是基本的想法。当您遇到重复设置时,后面的值将替换集合中较旧的值。要编写文件,只需输出reg标头,然后迭代集合,编写*密钥名称,然后是该密钥的所有值。

#1


For a first try i would read this .reg file into a tree, just the way regedit looks like. By building up the tree you just check if a node exists. If not, created it else overwrite the existing value.

第一次尝试我会将这个.reg文件读入树中,就像regedit的样子一样。通过构建树,您只需检查节点是否存在。如果没有,则创建它否则覆盖现有值。

So you'll get just the last entry which take into effect. After creating this tree, you'll have to read back the current tree and you're ready.

因此,您将获得生效的最后一个条目。创建此树后,您必须回读当前树,然后就可以了。

While writing this, i'll got another idea: Open the .reg file in a texteditor. Replace "[HKEY_CURRENT_USER\" against "[HKEY_CURRENT_USER\JustSomeKeyThatDoesNotExists" and import this file into the registry. Now open the registry and export the whole "[HKEY_CURRENT_USER\JustSomeKeyThatDoesNotExists" back into a file. Open the file and make the first replacement backwards.

在写这篇文章时,我会有另一个想法:打开texteditor中的.reg文件。将“[HKEY_CURRENT_USER”替换为“[HKEY_CURRENT_USER \ JustSomeKeyThatDoesNotExists”并将此文件导入注册表。现在打开注册表并将整个“[HKEY_CURRENT_USER \ JustSomeKeyThatDoesNotExists”]导回到文件中。打开文件并向后进行第一次更换。

Now you'll have a file with unique value in a sorted order. ;-))

现在,您将按排序顺序拥有一个具有唯一值的文件。 ;-))

#2


You could use a Dictionary of Dictionaries. In pseudo code, you could then do something like this:

您可以使用词典词典。在伪代码中,您可以执行以下操作:

Dictionary<String, Dictionary<String, String>> regSettings
Dictionary<String, String> current
for line in sourcefile {
    if line.startswith("[") {
        if !regSettings.haskey(line) {
            regSettings.add(line, new Dictionary<String, String>() )
        }
        current = regSettings[line]
    } else {
        key, value = line.split("=")
        current[key] = value;
    }
}

There are some details to account for (blank lines, comments, poorly remembered function names that should be adjusted for C#...), but that's the basic idea. When you come across a duplicate setting, the later value will replace the older one in the collection. To write the file, just output the reg header, then iterate your collections, writing the top-level key name, followed by all values for that key.

有一些细节需要考虑(空白行,注释,记忆不佳的函数名称应该为C#调整......),但这是基本的想法。当您遇到重复设置时,后面的值将替换集合中较旧的值。要编写文件,只需输出reg标头,然后迭代集合,编写*密钥名称,然后是该密钥的所有值。