/// <summary>Registers or unregisters Visual Studio custom tool identified by <see cref="Guid"/>, its attributes, Visual Studio versions and <see cref="Guid"/> of languages.</summary> /// <param name="Guid">Guid of custom tool class</param> /// <param name="attribute"><see cref="CustomToolAttribute"/> providing custom tool name, desription and flags.</param> /// <param name="VSVersions">Versions of Visual Studio to register/unregister tool for (those are names of keys under HKLM\Software\Microsoft\VisualStudio)</param> /// <param name="Languages"><see cref="Guid">Guids</see> of languages to register/unregister tool for (those are names of key unser HKLM\Software\Microsoft\VisualStudio\<version>\Generators)</param> /// <param name="Register">True to register the tool, false to unregister it</param> /// <exception cref="ArgumentException"><paramref name="Guid"/> is <see cref="Guid.Empty"/>, name of custom tool cases that name of registry sub-key is longer tham maximum allowed lenght (255 chars)</exception> /// <exception cref="ArgumentNullException"><paramref name="attribute"/>, <paramref name="VSVersions"/> or <paramref name="Languages"/> is null</exception> /// <exception cref="System.Security.SecurityException">The user does not have the permissions required to create or open the registry key HKLM\Software\Microsoft\VisualStudio\<version>\Generators\<language>\<name></exception> /// <exception cref="System.UnauthorizedAccessException">Cannot write to registry key (The user does not have the necessary access rights.)</exception> public static void RegisterCustomTool(Guid Guid, CustomToolAttribute attribute, string[] VSVersions, Guid[] Languages, bool Register) { if (Guid == Guid.Empty) { throw new ArgumentException(ResourcesT.ExceptionsVsCs.GuidCannotBeEmpty, "Guid"); } if (attribute == null) { throw new ArgumentNullException("attribute"); } if (VSVersions == null) { throw new ArgumentNullException("VSVersions"); } if (Languages == null) { throw new ArgumentNullException("Languages"); } foreach (string vsVer in VSVersions) { foreach (Guid Lang in Languages) { string keyName = string.Format("Software\\Microsoft\\VisualStudio\\{0}\\Generators\\{1:B}\\{2}", vsVer, Lang, attribute.Name); if (Register) { using (RegistryKey key = Registry.LocalMachine.CreateSubKey(keyName)) { key.SetValue("", attribute.Description); key.SetValue("CLSID", Guid.ToString("B")); key.SetValue("GeneratesDesignTimeSource", attribute.GeneratesDesignTimeSource ? 1 : 0, RegistryValueKind.DWord); if (attribute.GeneratesDesignTimeSharedSource) { key.SetValue("GeneratesDesignTimeSharedSource", 1, RegistryValueKind.DWord); } } } else {//Unregister Registry.LocalMachine.DeleteSubKey(keyName); } } } }
/// <summary>Registers or unregisters Visual Studio custom tool identified by <see cref="Guid"/>, its attributes, Visual Studio versions and <see cref="Guid"/> of languages.</summary> /// <param name="Guid">Guid of custom tool class</param> /// <param name="attribute"><see cref="CustomToolAttribute"/> providing custom tool name, desription and flags.</param> /// <param name="VSVersions">Versions of Visual Studio to register/unregister tool for (those are names of keys under HKLM\Software\Microsoft\VisualStudio)</param> /// <param name="Languages"><see cref="Guid">Guids</see> of languages to register/unregister tool for (those are names of key unser HKLM\Software\Microsoft\VisualStudio\<version>\Generators)</param> /// <param name="Register">True to register the tool, false to unregister it</param> /// <exception cref="ArgumentException"><paramref name="Guid"/> is <see cref="Guid.Empty"/>, name of custom tool cases that name of registry sub-key is longer tham maximum allowed lenght (255 chars)</exception> /// <exception cref="ArgumentNullException"><paramref name="attribute"/>, <paramref name="VSVersions"/> or <paramref name="Languages"/> is null</exception> /// <exception cref="System.Security.SecurityException">The user does not have the permissions required to create or open the registry key HKLM\Software\Microsoft\VisualStudio\<version>\Generators\<language>\<name></exception> /// <exception cref="System.UnauthorizedAccessException">Cannot write to registry key (The user does not have the necessary access rights.)</exception> public static void RegisterCustomTool(Guid Guid, CustomToolAttribute attribute, string[] VSVersions, Guid[] Languages, bool Register) { if (Guid == Guid.Empty) throw new ArgumentException(ResourcesT.ExceptionsVsCs.GuidCannotBeEmpty, "Guid"); if (attribute == null) throw new ArgumentNullException("attribute"); if (VSVersions == null) throw new ArgumentNullException("VSVersions"); if (Languages == null) throw new ArgumentNullException("Languages"); foreach (string vsVer in VSVersions) foreach (Guid Lang in Languages) { string keyName = string.Format("Software\\Microsoft\\VisualStudio\\{0}\\Generators\\{1:B}\\{2}", vsVer, Lang, attribute.Name); if (Register) { using (RegistryKey key = Registry.LocalMachine.CreateSubKey(keyName)) { key.SetValue("", attribute.Description); key.SetValue("CLSID", Guid.ToString("B")); key.SetValue("GeneratesDesignTimeSource", attribute.GeneratesDesignTimeSource ? 1 : 0, RegistryValueKind.DWord); if (attribute.GeneratesDesignTimeSharedSource) key.SetValue("GeneratesDesignTimeSharedSource", 1, RegistryValueKind.DWord); } } else {//Unregister Registry.LocalMachine.DeleteSubKey(keyName); } } }