Skip to content

Commit

Permalink
Fixes after switching to AsyncPackage
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Jun 21, 2018
1 parent 98ef507 commit 0c62174
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 72 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Visual Studio extension to run Visual Studio as administrator

## Run Visual Studio as administrator
1. Install the [RunAsAdmin](https://marketplace.visualstudio.com/items?itemName=SamirBoulema.RunAsAdmin) extension
2. Restart Visual Studio
3. Visual Studio will be run as administrator
2. Go to Tools -> Options -> RunAsAdmin
3. Set Enabled to true
4. Restart Visual Studio

## Run Visual Studio as normal user
1. Go to Tools -> Options -> RunAsAdmin
Expand Down
9 changes: 9 additions & 0 deletions RunAsAdmin/DteHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using EnvDTE;

namespace SamirBoulema.RunAsAdmin
{
public static class DteHelper
{
public static DTE Dte;
}
}
1 change: 1 addition & 0 deletions RunAsAdmin/RunAsAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="DteHelper.cs" />
<Compile Include="Guids.cs" />
<Compile Include="Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
82 changes: 14 additions & 68 deletions RunAsAdmin/RunAsAdminPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,47 @@
using System.Runtime.InteropServices;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.Win32;

namespace SamirBoulema.RunAsAdmin
{
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[ProvideAutoLoad(UIContextGuids80.NoSolution, PackageAutoLoadFlags.BackgroundLoad)]
[Guid(GuidList.guidRunAsAdminPkgString)]
[ProvideOptionPage(typeof(OptionPageGrid), "RunAsAdmin", "General", 0, 0, true)]
public sealed class RunAsAdminPackage : AsyncPackage
{
private DTE _dte;
private string _devenvFilename;
private const string RegistryFolder = @"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\";

protected override async System.Threading.Tasks.Task InitializeAsync(System.Threading.CancellationToken cancellationToken,
IProgress<ServiceProgressData> progress)
{
_dte = await GetServiceAsync(typeof(DTE)) as DTE;

DteHelper.Dte = await GetServiceAsync(typeof(DTE)) as DTE;
await base.InitializeAsync(cancellationToken, progress);

var options = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid));
options.SetPackage(this);
_devenvFilename = _dte.Application.FileName;

var regEntry = Registry.GetValue(RegistryFolder, _devenvFilename, null);

if (options.Enabled == false && regEntry == null ||
options.Enabled && (regEntry == null || !regEntry.Equals("RUNASADMIN")))
{
options.Enabled = true;
options.SaveSettingsToStorage();
RestartVisualStudio();
}
}

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");
}
}
else
{
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 sealed class OptionPageGrid : DialogPage
{
private RunAsAdminPackage _package;

public void SetPackage(RunAsAdminPackage package)
{
_package = package;
}

private bool _enabled;
private const string RegistryFolder = @"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\";

[Category("RunAsAdmin")]
[DisplayName(@"Enabled")]
[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);
}
get => IsAdminEnabled();
set => RunAsAdmin(value);
}

private void RunAsAdmin(bool enable)
=> Registry.SetValue(RegistryFolder, DteHelper.Dte.Application.FileName, (enable ? "RUNASADMIN" : "RUNASNORMAL"));

private bool IsAdminEnabled()
{
var runAsAdminKeyDevEnv = Registry.GetValue(RegistryFolder, DteHelper.Dte.Application.FileName, null) as string;

return !string.IsNullOrEmpty(runAsAdminKeyDevEnv) && runAsAdminKeyDevEnv.Equals("RUNASADMIN");
}
}
}
2 changes: 1 addition & 1 deletion RunAsAdmin/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?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="dda2a29d-8fd9-4131-adc5-afcf6ef36b25" Version="1.4" Language="en-US" Publisher="Samir L. Boulema" />
<Identity Id="dda2a29d-8fd9-4131-adc5-afcf6ef36b25" Version="1.5" Language="en-US" Publisher="Samir L. Boulema" />
<DisplayName>RunAsAdmin</DisplayName>
<Description xml:space="preserve">Run Visual Studio as administrator</Description>
<MoreInfo>https://github.com/sboulema/RunAsAdmin</MoreInfo>
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.4.{build}
version: 1.5.{build}
skip_tags: true
configuration:
- Release
Expand Down

0 comments on commit 0c62174

Please sign in to comment.