5

I'm automating the deployment of a Windows Store App and I'd like to automatically set one of the settings that a user would normally configure in the settings charm. I've learned that the settings are stored in a settings.dat file, which can be opened in the registry. But this is a binary file and I have no idea how I would edit the setting I want through Powershell. Is this even something I can do or is the effort not worth it? Thanks.

This is what the particular setting looks like regedit enter image description here

3
  • If this is about setting a value in the registry then you should be able to use the .net registry APIs with PowerShell... Jan 16, 2015 at 23:39
  • Well, the setting isn't actually in the registry. It's in a file that can be read by the registry, but in a standard install it's just sitting in a folder somewhere.
    – NSouth
    Jan 16, 2015 at 23:51
  • Oh, sorry, never heard the registry can be used to read files. Jan 17, 2015 at 0:12

1 Answer 1

7

AFAIK there's no way to edit registry files when they are not loaded into the registry. I played with it for a while and found a way to do it, but you need to temporarily load the registry file into your registry and edit it there. It seems you need to use reg.exe to do this.

The other problem are custom property types which are used in this registry file (5f5e10c instead of e.g. REG_BINARY). Both PowerShell and .NET APIs don't seem to be able to load them or correctly save them. I had to export the keys, edit them in .reg file and import them back.

Another peculiarity to take into account is the timestamp included in all the encoded values, as described in this blog post.

Here's a working script I managed to write with additional explanation in the comments (you need to run it as administrator or loading the registry file will fail):

# full path to the registry file to edit
$settingsFile = "c:\Users\Damir\AppData\Local\Packages\113f4f59-2aa3-455b-8531-185f633c1ffe_ecet6zh215f6e\Settings\settings.dat"
# setting name to change
$settingKey = "ServerUrl"
# new setting value
$newValue = "http://prodserver.local/esign/"

# name of temporary .reg file
$regFile = ".\settings.reg"
# temporary registry location to import registry file into
$registryImportLocation = "HKLM\_TMP"

# prefix matching the setting in the .reg file
$settingKeyPattern = """$settingKey""="

# load the settings file into registry
reg load $registryImportLocation $settingsFile
# export the settings into .reg file
reg export $registryImportLocation $regFile

$fileContents = Get-Content $regFile

$finalContents = @()
$processing = $false
Foreach ($line in $fileContents) 
{ 
    If (-Not ($processing))
    {
        If ($line.StartsWith($settingKeyPattern))
        {
            # setting key found, stop copying file contents
            $processing = $true
            # read key value without its name
            $oldSerializedValue = $line.Replace($settingKeyPattern, "")
        }
        Else
        {
            # setting key not found yet, copy old file contents to output
            $finalContents += $line
        }
    }
    else
    {
        # value can span multiple lines, trim leading spaces from non-first lines
        $oldSerializedValue += $line.TrimStart(" ")
    }
    If ($processing)
    {
        If ($oldSerializedValue.EndsWith("\"))
        {
            # trailing \ indicates non-final line with key value
            $oldSerializedValue = $oldSerializedValue.TrimEnd("\")
        }
        Else
        {
            # split key type and value
            $match = $oldSerializedValue -match '(.*:)(.*)'

            # final 8 bytes are timestamp of the value - don't modify
            $timestamp = $matches[2].Substring($matches[2].Length - 23)

            # encode new value in UTF-16
            $newValueInBytes = [System.Text.Encoding]::Unicode.GetBytes($newValue)

            # key name and type
            $newValue = $settingKeyPattern + $matches[1]
            # encode byte array into required string format
            Foreach ($byte in $newValueInBytes)
            {
                $newValue += [System.Convert]::ToString($byte, 16).PadLeft(2, "0") + ","
            }
            # end null character string terminator
            $newValue += "00,00," + $timestamp

            $finalContents += $newValue
            # reenable copying of the remaining file
            $processing = $false
        }
    }
}

# dump new file contents to file
$finalContents | Out-File $regFile

# import the changed value into registry
reg import $regFile
# onload the registry file from registry
reg unload $registryImportLocation

# delete temporary file
Remove-Item $regFile

You'll need to modify it a bit to include it in your deployment process, but it should get you started.

EDIT: I wrote an accompanying blog post describing the thought process behind the answer. It provides an even more in depth explanation and links to a PowerShell function implementation wrapping the above script.

4
  • Very informative and helpful answer! It works like a charm to edit my settings. However, I'm hoping to set initial settings after a deployment and I've found that the settingKey does not exist in the file until the user saves a setting. So my challenge is to add the key and value versus replacing an existing value. This is tough because of the timestamp and stuff. This isn't super high priority on my project, but is a nice to have.
    – NSouth
    Jan 19, 2015 at 17:54
  • 1
    @NSouth maybe you could try including an initial settings.dat file with your distribution and copying it into the right folder as part of the deployment. Not sure if timestamp value would be valid after doing this, though.
    – Damir Arh
    Jan 19, 2015 at 18:57
  • That's what I was just considering. I'm swamped now, but will try it when I can and come back here to report and accept your answer. Thanks!
    – NSouth
    Jan 19, 2015 at 21:30
  • I have successfully applied an existing settings.dat file during deployment. It works perfectly. Thanks for all your help!
    – NSouth
    Jan 21, 2015 at 13:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.