|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWhen I use the Visual Studio environment, sometimes I have to open 'Windows Explorer' in the folder of the solution to do some tasks as copy files, rename files, launch application by double clicking on the executable. Because my projects aren't at the root of my hard disk, I lose a lot of time. So, one day, I decided to create the 'Explorer' add-in for Visual Studio 2005. Install the add-inTo install the 'Explorer' add-in:
Using the Add-InTo open 'Windows Explorer' in a specific folder:
To launch the application as if you double clicked on an '.exe' file in 'Windows Explorer'.
To collapse all nodes of the solution explorer window
To search help on the web for the current selected source code
Popup Menu CreationThis part is done by the In Visual Studio, the private CommandBar GetCommandBar(string commandBarName)
{
return ((CommandBars)application.DTE.CommandBars)[commandBarName];
}
where CommandBarPopup menu = GetCommandBar(commandBarName).Controls.Add(
MsoControlType.msoControlPopup,
Missing.Value, Missing.Value, 1, true)
as CommandBarPopup;
Command CreationThis part is done by the
Menu Item Handlers (details)When creating the menu items, the private void MenuItem_Click(object commandBarControl,
ref bool handled, ref bool cancelDefault)
{
CommandBarControl menuItem =
(CommandBarControl)commandBarControl;
if (cmdList.ContainsKey(menuItem.Tag))
{
cmdList[menuItem.Tag].Perform();
}
}
OpenFolderCommandThis command is used to open your file manager in a folder corresponding to the selected item in the 'Solution Explorer' window. To do it, I call the Configuration appConfig =
ConfigurationManager.OpenExeConfiguration(
Assembly.GetExecutingAssembly().Location);
System.Diagnostics.Process.Start(GetFileManagerPath(appConfig),
GetParameter(appConfig) + folderPath);
where
ExecuteCommandThis command is used to launch the application corresponding to the selected project as if you double click on the ".exe" file in the 'Windows Explorer'. To do it, I call the System.Diagnostics.Process.Start(assemblyPath);
The assembly path is found by using the following code: private string GetAssemblyPath(EnvDTE.Project project)
{
Property outputPath =
project.ConfigurationManager.ActiveConfiguration.
Properties.Item("OutputPath");
Property assemblyName = project.Properties.Item("OutputFileName");
return GetFolderPath(project.FullName) + "\\" +
(string)outputPath.Value + (string)assemblyName.Value;
}
CollapseCommandThis command is used to collapse al the nodes of the Solution Explorer window. To do it, I search (recursively) for all child nodes (of the root node) and then loop on to set their ' private void Collapse(UIHierarchyItem item, ref UIHierarchy solutionExplorer)
{
foreach (UIHierarchyItem innerItem in item.UIHierarchyItems)
{
if (innerItem.UIHierarchyItems.Count > 0)
{
// Recursive call
Collapse(innerItem, ref solutionExplorer);
// Collapse
if (innerItem.UIHierarchyItems.Expanded)
{
innerItem.UIHierarchyItems.Expanded = false;
if (innerItem.UIHierarchyItems.Expanded)
{
// Bug in VS 2005
innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect);
solutionExplorer.DoDefaultAction();
}
}
}
}
}
SearchHelpOnWebCommandThis command is used to search help for the currently selected source code on web sites which are defined in the 'SearchEngineEurls.xml' file. For each <?xml version="1.0" encoding="utf-8"?>
<SearchEngineUrlsFile
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SearchEngineUrls>
<SearchEngineUrl Name="Google"
Url="www.google.fr/search?q=" />
<SearchEngineUrl Name="Msdn"
Url="http://search.microsoft.com/search/
results.aspx?view=msdn&q="/>
</SearchEngineUrls>
</SearchEngineUrlsFile>
To do it, I get the current 'Selection' using the following code : selection = ((TextSelection)Application.DTE.ActiveWindow.Selection)
If no text is selected, I programmatically select the nearest word of the cursor. Then, I concatenate the 'search engine URL' which corresponds to the selected menu item (MSDN or Google or ...) and the selected text => this gives me the complete search URL. Finally, I just open this URL in a new tab in the Visual Studio Environment like this : Application.DTE.ItemOperations.Navigate(
SearchEngineUrl + HttpUtility.UrlEncode(selectedText),
vsNavigateOptions.vsNavigateOptionsNewWindow);
Debug commands: LogCommandBarsCommand and LogItemPropertiesCommandTo develop this add-in, I had to:
Log4NetLog4Net is a port of the excellent Log4J logging framework (see this website for more explanations). Here, I use it to log in a file all the commands selection by the user. To do it, I configure Log4Net in the app.config file like this: <log4net>
<!-- Define level and appender to use for root logger -->
<root>
<level value="ALL" /> <!-- can be one of the following :
ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
<appender-ref ref="FileAppender" />
</root>
<!-- Define appender -->
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\ExplorerAddin.log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
Then, I only have to declare a logger:
protected static readonly ILog _logger =
LogManager.GetLogger((MethodBase.GetCurrentMethod().DeclaringType));
and use it: _logger.DebugFormat("Starting to open " +
"the following folder : '{0}'", folderPath);
History
ConclusionI hope you will enjoy this add-in.
| |||||||||||||||||||||