Created
January 14, 2015 10:06
-
-
Save davidwhitney/e96f6fc5659975252be0 to your computer and use it in GitHub Desktop.
Manipulate app compatibility flags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Configuration.Abstractions; | |
using System.IO; | |
using log4net; | |
using Microsoft.Win32; | |
namespace Doco.Conversion | |
{ | |
public class BackwardsCompatabilityPatchForWindowsServer : IBackwardsCompatabilityPatchForWindowsServer | |
{ | |
private static readonly ILog Log = LogManager.GetLogger(typeof(BackwardsCompatabilityPatchForWindowsServer)); | |
private readonly IConfigurationManager _config; | |
private readonly Paths _paths; | |
public BackwardsCompatabilityPatchForWindowsServer(IConfigurationManager config, Paths paths) | |
{ | |
_config = config; | |
_paths = paths; | |
} | |
public bool PatchIsRequired() | |
{ | |
return _config.AppSettings.AppSetting("Wordconv.EmulateWindowsXp", () => true); | |
} | |
public void ForceApplicationCompatabilityMode(string filePath, bool forAllSystemUsers = false) | |
{ | |
var wordConvPath = Path.Combine(_paths.CurrentApplicationPath, filePath); | |
Log.Debug("Backwards compatibility patch is required, setting registry key..."); | |
Log.Debug("Codebase: " + _paths.CurrentApplicationPath); | |
Log.Debug("WordConv.exe path: " + wordConvPath); | |
var rootKey = forAllSystemUsers ? Registry.LocalMachine : Registry.CurrentUser; | |
using (var appCompatFlags = OpenOrCreate(rootKey, "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags")) | |
using (var layers = OpenOrCreate(appCompatFlags, "Layers")) | |
{ | |
layers.SetValue(wordConvPath, "~ WINXPSP3", RegistryValueKind.String); | |
Log.Debug("Set Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\\" + wordConvPath + " to ~ WINXPSP3"); | |
} | |
} | |
private static RegistryKey OpenOrCreate(RegistryKey parent, string path) | |
{ | |
var key = parent.OpenSubKey(path, true) ?? parent.CreateSubKey(path); | |
if (key == null) | |
{ | |
throw NotSupported(); | |
} | |
return key; | |
} | |
private static NotSupportedException NotSupported() | |
{ | |
return new NotSupportedException("Attempted to set compatability mode for but registry unavailable on platform."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment