My fourth How do I is up.
Scenario
Let's say that we want need to check if Visual Studio is running in elevated mode because there are some specific actions that we need to perform that need those permissions. The focus of this How do I tackles this scenario.
Interfaces and classes needed
Code snippet
IVsShell3 shell = serviceProvider.GetService(typeof(SVsShell)) as IVsShell3;
bool elevated;
shell.IsRunningElevated(out elevated);
if(!elevated)
{
//Visual Studio is not running in elevated mode, let's restart it
shell.RestartElevated();
}
Assemblies needed
- Microsoft.VisualStudio.Shell.Interop.9.0
Stay tuned,
Pablo
My third How do I is up.
Scenario
Let's say that we want to intercept the "Clean Solution" command. For a particular kind of project, before executing the command we need to clean other resources and instead of doing it with msbuild we want to do it by intercepting the command at the VS level. The focus of this How do I tackles this scenario.
Interfaces and classes needed
Code snippet
public class VSCommandInterceptor : IDisposable
{
private IServiceProvider serviceProvider;
private Guid guid;
private int id;
private bool isDisposed;
public VSCommandInterceptor(IServiceProvider serviceProvider, Guid commandGuid, int commandId)
{
this.serviceProvider = serviceProvider;
this.guid = commandGuid;
this.id = commandId;
if(CommandEvents != null)
{
CommandEvents.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(OnAfterExecute);
CommandEvents.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(OnBeforeExecute);
}
}
public event EventHandler<EventArgs> AfterExecute;
public event EventHandler<EventArgs> BeforeExecute;
private CommandEvents commandEvents;
protected CommandEvents CommandEvents
{
get
{
if(commandEvents == null)
{
DTE dte = this.serviceProvider.GetService(typeof(DTE)) as DTE;
if(dte != null)
{
commandEvents = dte.Events.get_CommandEvents(guid.ToString("B"), id) as CommandEvents;
}
}
return commandEvents;
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if(!this.isDisposed && disposing)
{
if(CommandEvents != null)
{
CommandEvents.AfterExecute -= OnAfterExecute;
CommandEvents.BeforeExecute -= OnBeforeExecute;
}
this.isDisposed = true;
}
}
private void OnAfterExecute(string Guid, int ID, object CustomIn, object CustomOut)
{
if(AfterExecute != null)
{
AfterExecute(this, new EventArgs());
}
}
private void OnBeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
if(BeforeExecute != null)
{
BeforeExecute(this, new EventArgs());
}
}
}
Usage
VSCommandInterceptor interceptor =
new VSCommandInterceptor(
serviceProvider,
typeof(Microsoft.VisualStudio.VSConstants.VSStd97CmdID).GUID,
(int)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.CleanSln);
interceptor.BeforeExecute += new EventHandler<EventArgs>(BeforeExecute);
private void BeforeExecute(object sender, EventArgs e)
{
//TODO: Provide logic
}
Assemblies needed
Stay tuned,
Pablo
Today I was browsing Youtube and I accidentally found a demo of PowerCommands
PowerCommands has almost 25k downloads and it is by far the most downloaded tool in the Visual Studio Gallery.
Pablo