Skip to content

Instantly share code, notes, and snippets.

@davidwhitney
Created January 14, 2015 10:06
Show Gist options
  • Save davidwhitney/e96f6fc5659975252be0 to your computer and use it in GitHub Desktop.
Save davidwhitney/e96f6fc5659975252be0 to your computer and use it in GitHub Desktop.
Manipulate app compatibility flags
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