Skip to content

Commit

Permalink
Switch MS account, Improve switching setting
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Jun 20, 2016
1 parent d6b9ae5 commit 138b817
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
RunAsAdmin.sln.ide/
RunAsAdmin/bin/
RunAsAdmin/obj/
/RunAsAdmin/*.user
/.vs
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 6 additions & 1 deletion RunAsAdmin.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion RunAsAdmin/Guids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// MUST match guids.h
using System;

namespace FundaRealEstateBV.RunAsAdmin
namespace SamirBoulema.RunAsAdmin
{
static class GuidList
{
Expand Down
9 changes: 4 additions & 5 deletions RunAsAdmin/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 19 additions & 9 deletions RunAsAdmin/Resources/License.txt
Original file line number Diff line number Diff line change
@@ -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.
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.
7 changes: 2 additions & 5 deletions RunAsAdmin/RunAsAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ProjectTypeGuids>{82b43b9b-a64c-4715-b499-d71e9ca2bd60};{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FundaRealEstateBV.RunAsAdmin</RootNamespace>
<RootNamespace>SamirBoulema.RunAsAdmin</RootNamespace>
<AssemblyName>RunAsAdmin</AssemblyName>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>
Expand Down Expand Up @@ -151,10 +151,7 @@
<None Include="Key.snk" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\License.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Resource Include="Resources\License.txt" />
<Content Include="Resources\run.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
Expand Down
75 changes: 57 additions & 18 deletions RunAsAdmin/RunAsAdminPackage.cs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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");
}
Expand All @@ -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);
}
}
}
}
3 changes: 1 addition & 2 deletions RunAsAdmin/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="f6211522-a083-49ba-aba4-9c082ab8a570" Version="1.0" Language="en-US" Publisher="Funda Real Estate B.V." />
<Identity Id="dda2a29d-8fd9-4131-adc5-afcf6ef36b25" Version="1.1" Language="en-US" Publisher="Samir L. Boulema" />
<DisplayName>RunAsAdmin</DisplayName>
<Description>Run Visual Studio as administrator</Description>
<MoreInfo>http://visualstudiogallery.msdn.microsoft.com/3f911864-03e1-4b91-9578-4925d5966193</MoreInfo>
<License>Resources\License.txt</License>
<Icon>Resources\run.ico</Icon>
<PreviewImage>Resources\run.jpg</PreviewImage>
<Tags>Run As, Admin, Administrator, Root</Tags>
Expand Down

0 comments on commit 138b817

Please sign in to comment.