tags:

views:

610

answers:

2

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.

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"

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"

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.

A: 

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.

JimG
+2  A: 

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.

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.

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

Oliver
Like your second idea really good that tried it and it worked great.
PeteT