private static void Install() { string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); string preSharpDir = Path.Combine(programFiles, "PreSharp"); string preSharpTargetsFile = Path.Combine(preSharpDir, "PreSharp.targets"); string preSharpExecutable = Path.Combine(preSharpDir, "PreSharp.exe"); var versionsDic = new Dictionary <string, string> { { "v3.5", "9.0" }, { "v4.0", "10.0" } }; //Setup PreSharp Directory.CreateDirectory(preSharpDir); if (!AssemblyUtils.CopyTo(preSharpExecutable)) { Console.WriteLine("PreSharp " + AssemblyUtils.GetVersion().ToString() + " is already installed. Only updating system configuration."); } string preSharpTargetsFileContents = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("PreSharp.PreSharp.targets")).ReadToEnd(); File.WriteAllText(preSharpTargetsFile, preSharpTargetsFileContents); //For each .net version foreach (var version in versionsDic) { string msBuildExtensionsDir = Path.Combine(programFiles, @"MsBuild\" + version.Key); string customAfterMicrosoftCommonTargetsFile = Path.Combine(msBuildExtensionsDir, "Custom.After.Microsoft.Common.targets"); string visualStudioKeyPath = @"SOFTWARE\Microsoft\VisualStudio\" + version.Value; //They dont seem to decide where to keep the stuff... string[] dotNetKeyPaths = new string[] { @"SOFTWARE\Microsoft\NET Framework Setup\NDP\" + version.Key, @"SOFTWARE\Microsoft\NET Framework Setup\NDP\" + version.Key.Substring(0, Math.Max(version.Key.LastIndexOf('.'), 0)) + @"\Full\" }; bool foundDotNet = false; foreach (string dotNetKeyPath in dotNetKeyPaths) { using (var dotNetKey = Registry.LocalMachine.OpenSubKey(dotNetKeyPath, false)) { if (dotNetKey != null && ((1).Equals(dotNetKey.GetValue("Install")) || (1).Equals(dotNetKey.GetValue("Full")))) { foundDotNet = true; break; } } } if (!foundDotNet) { continue; } //Setup MSBUILD Directory.CreateDirectory(msBuildExtensionsDir); XElement import = new XElement(XName.Get("Import", string.Empty), new XAttribute("Project", @"$(ProgramFiles)\PreSharp\PreSharp.targets"), new XAttribute("Condition", @" Exists('$(ProgramFiles)\PreSharp\PreSharp.targets') and Exists('$(ProgramFiles)\PreSharp\PreSharp.exe') and '$(DISABLE_PRESHARP)' == '' ")); XElement project; if (!File.Exists(customAfterMicrosoftCommonTargetsFile)) { project = new XElement("Project", import); } else { project = XElement.Parse(Regex.Replace(File.ReadAllText(customAfterMicrosoftCommonTargetsFile), @"<Project\s*xmlns\s*=\s*""http://[^""]*""\s*>", "<Project>")); if (!project.Elements("Import").Attributes("Project").Where(attr => attr.Value == @"$(ProgramFiles)\PreSharp\PreSharp.targets").Any()) { project.Add(import); } } File.WriteAllText(customAfterMicrosoftCommonTargetsFile, project.ToString().Replace("<Project>", "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">")); //Setup VS bool installedInVs = false; using (var vsKey = Registry.LocalMachine.OpenSubKey(visualStudioKeyPath, false)) { if (vsKey != null && vsKey.GetValue("InstallDir") != null) { using (var key = Registry.LocalMachine.OpenSubKey(visualStudioKeyPath + @"\MSBuild\SafeImports", true)) { if (key != null) { key.SetValue("PreSharp", preSharpTargetsFile); } } using (var key = Registry.LocalMachine.OpenSubKey(visualStudioKeyPath + @"\Languages\File Extensions\.cst", true)) { if (key == null) { using (var newKey = Registry.LocalMachine.OpenSubKey(visualStudioKeyPath + @"\Languages\File Extensions", true).CreateSubKey(".cst")) { newKey.SetValue(null, "{694DD9B6-B865-4C5B-AD85-86356E9C88DC}"); } } } installedInVs = true; } } Console.WriteLine("PreSharp " + AssemblyUtils.GetVersion().ToString() + " was successfully installed in .net " + version.Key + (installedInVs ? "" : " (no Visual Studio)")); } }