From 138b817b13a1cb48cf805986ab3825c3793cff7e Mon Sep 17 00:00:00 2001 From: "Samir L. Boulema" Date: Mon, 20 Jun 2016 16:17:30 +0200 Subject: [PATCH] Switch MS account, Improve switching setting --- .gitignore | 2 + README.md | 21 +++++++ RunAsAdmin.sln | 7 ++- RunAsAdmin/Guids.cs | 2 +- RunAsAdmin/Resources.Designer.cs | 9 ++- RunAsAdmin/Resources/License.txt | 28 ++++++--- RunAsAdmin/RunAsAdmin.csproj | 7 +-- RunAsAdmin/RunAsAdminPackage.cs | 75 ++++++++++++++++++------ RunAsAdmin/source.extension.vsixmanifest | 3 +- 9 files changed, 113 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index 2e9deb1..585e9e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ RunAsAdmin.sln.ide/ RunAsAdmin/bin/ RunAsAdmin/obj/ +/RunAsAdmin/*.user +/.vs diff --git a/README.md b/README.md index 5fce77b..fea65f4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,23 @@ # RunAsAdmin Visual Studio extension to run Visual Studio as administrator + +## Run Visual Studio as administrator +1. Install the extension +2. Restart Visual Studio +3. Visual Studio will be run as administrator + +## Run Visual Studio as normal user +1. Go to Tools -> Options -> RunAsAdmin +2. Set Enabled to false +3. Restart Visual Studio + +## Uninstalling the extension +When uninstalling the extension the current elevation mode will be preserved. If you have the extension Enabled in the options and uninstall it, Visual Studio will remain running as administrator. You have to use the RunAsAdmin options setting to enable or disable. + +## Disabling the extension +When disabling the extension from within the Visual Studio extensions dialog nothing will happen like with uninstalling the extension. You have to use the RunAsAdmin options setting to enable or disable. + +### Under the hood +The extension adds a registry entry at the following location: `HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\` + +Where the "Name" will be the path to Visual Studio and the value will be "RUNASADMIN" or "RUNASNORMAL" \ No newline at end of file diff --git a/RunAsAdmin.sln b/RunAsAdmin.sln index 031602c..7116bd3 100644 --- a/RunAsAdmin.sln +++ b/RunAsAdmin.sln @@ -1,10 +1,15 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.22013.1 +VisualStudioVersion = 14.0.25402.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RunAsAdmin", "RunAsAdmin\RunAsAdmin.csproj", "{62B3201D-A451-4A9E-B956-E44AD7152787}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{34FFEFE9-DE5F-45DF-A277-B92ED6A1C66F}" + ProjectSection(SolutionItems) = preProject + README.md = README.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/RunAsAdmin/Guids.cs b/RunAsAdmin/Guids.cs index 9998864..defe607 100644 --- a/RunAsAdmin/Guids.cs +++ b/RunAsAdmin/Guids.cs @@ -2,7 +2,7 @@ // MUST match guids.h using System; -namespace FundaRealEstateBV.RunAsAdmin +namespace SamirBoulema.RunAsAdmin { static class GuidList { diff --git a/RunAsAdmin/Resources.Designer.cs b/RunAsAdmin/Resources.Designer.cs index 0075a59..b2176e1 100644 --- a/RunAsAdmin/Resources.Designer.cs +++ b/RunAsAdmin/Resources.Designer.cs @@ -1,14 +1,14 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:2.0.50727.42 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // //------------------------------------------------------------------------------ -namespace FundaRealEstateBV.RunAsAdmin { +namespace SamirBoulema.RunAsAdmin { using System; @@ -19,7 +19,7 @@ namespace FundaRealEstateBV.RunAsAdmin { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { @@ -39,7 +39,7 @@ internal Resources() { internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("FundaRealEstateBV.RunAsAdmin.Resources", typeof(Resources).Assembly); + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SamirBoulema.RunAsAdmin.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; @@ -59,6 +59,5 @@ internal Resources() { resourceCulture = value; } } - } } diff --git a/RunAsAdmin/Resources/License.txt b/RunAsAdmin/Resources/License.txt index fe881a0..2e3f3c4 100644 --- a/RunAsAdmin/Resources/License.txt +++ b/RunAsAdmin/Resources/License.txt @@ -1,11 +1,21 @@ -Copyright (c) 2014 Samir L. Boulema +The MIT License (MIT) -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +Copyright (c) 2016 Samir Boulema -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. \ No newline at end of file +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/RunAsAdmin/RunAsAdmin.csproj b/RunAsAdmin/RunAsAdmin.csproj index 812a58f..69f99b4 100644 --- a/RunAsAdmin/RunAsAdmin.csproj +++ b/RunAsAdmin/RunAsAdmin.csproj @@ -13,7 +13,7 @@ {82b43b9b-a64c-4715-b499-d71e9ca2bd60};{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} Library Properties - FundaRealEstateBV.RunAsAdmin + SamirBoulema.RunAsAdmin RunAsAdmin True Key.snk @@ -151,10 +151,7 @@ - - Always - true - + Always true diff --git a/RunAsAdmin/RunAsAdminPackage.cs b/RunAsAdmin/RunAsAdminPackage.cs index 1c9ef58..8727e1f 100644 --- a/RunAsAdmin/RunAsAdminPackage.cs +++ b/RunAsAdmin/RunAsAdminPackage.cs @@ -1,13 +1,12 @@ using System; +using System.ComponentModel; using System.Runtime.InteropServices; -using Microsoft.Win32; +using EnvDTE; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; -using EnvDTE; -using System.ComponentModel; -using System.IO; +using Microsoft.Win32; -namespace FundaRealEstateBV.RunAsAdmin +namespace SamirBoulema.RunAsAdmin { [PackageRegistration(UseManagedResourcesOnly = true)] [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] @@ -18,26 +17,33 @@ public sealed class RunAsAdminPackage : Package { private DTE _dte; private string _devenvFilename; - private OptionPageGrid _options; private const string RegistryFolder = @"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\"; protected override void Initialize() { base.Initialize(); - _options = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid)); + var options = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid)); + options.SetPackage(this); _dte = (DTE)GetService(typeof(DTE)); _devenvFilename = _dte.Application.FileName; - if (_options.Enabled == true) + var regEntry = Registry.GetValue(RegistryFolder, _devenvFilename, null); + + if ((options.Enabled == false && regEntry == null) || + (options.Enabled && (regEntry == null || !regEntry.Equals("RUNASADMIN")))) { - var RunAsAdminKeyLauncher = Registry.GetValue(RegistryFolder, @"C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe", null); - if (RunAsAdminKeyLauncher == null) - { - Registry.SetValue(RegistryFolder, @"C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe", "RUNASADMIN"); - } + options.Enabled = true; + options.SaveSettingsToStorage(); + RestartVisualStudio(); + } + } - var RunAsAdminKeyDevEnv = Registry.GetValue(RegistryFolder, _devenvFilename, null); - if (RunAsAdminKeyDevEnv == null || ((string)RunAsAdminKeyDevEnv).Equals("RUNASNORMAL")) + public void RunAsAdmin(bool enable) + { + if (enable) + { + var runAsAdminKeyDevEnv = Registry.GetValue(RegistryFolder, _devenvFilename, null); + if (runAsAdminKeyDevEnv == null || ((string)runAsAdminKeyDevEnv).Equals("RUNASNORMAL")) { Registry.SetValue(RegistryFolder, _devenvFilename, "RUNASADMIN"); } @@ -47,15 +53,48 @@ protected override void Initialize() Registry.SetValue(RegistryFolder, _devenvFilename, "RUNASNORMAL"); } } + + public void RestartVisualStudio() + { + // Start new instance of Visual Studio + try + { + System.Diagnostics.Process.Start(_dte.Application.FileName, _dte.Application.CommandLineArguments); + } + catch (Exception) + { + // User cancelled starting the new instance + } + + // Close old instance + _dte.ExecuteCommand("File.Exit"); + } } [ClassInterface(ClassInterfaceType.AutoDual)] [CLSCompliant(false), ComVisible(true)] - public class OptionPageGrid : DialogPage + public sealed class OptionPageGrid : DialogPage { + private RunAsAdminPackage _package; + + public void SetPackage(RunAsAdminPackage package) + { + _package = package; + } + + private bool _enabled; + [Category("RunAsAdmin")] [DisplayName(@"Enabled")] - [Description("Run Visual Studio as administrator")] - public bool Enabled { get; set; } + [Description("Run Visual Studio as administrator. Restart Visual Studio for the applied changes to take effect")] + public bool Enabled + { + get { return _enabled; } + set + { + _enabled = value; + _package?.RunAsAdmin(value); + } + } } } diff --git a/RunAsAdmin/source.extension.vsixmanifest b/RunAsAdmin/source.extension.vsixmanifest index 1aab915..d2a2b6f 100644 --- a/RunAsAdmin/source.extension.vsixmanifest +++ b/RunAsAdmin/source.extension.vsixmanifest @@ -1,11 +1,10 @@ - + RunAsAdmin Run Visual Studio as administrator http://visualstudiogallery.msdn.microsoft.com/3f911864-03e1-4b91-9578-4925d5966193 - Resources\License.txt Resources\run.ico Resources\run.jpg Run As, Admin, Administrator, Root