Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Friday, January 6, 2012

Listing all keyboard shortcuts in Visual Studio 2010

The NuGet package manager for Visual Studio comes with a hidden gem - a PowerShell console called Package Manager Console. This console not only allows developers to install and configure NuGet packages, but also has a useful variable called $dte, which is the Visual Studio automation COM object.

To demonstrate the usefulness of this feature, I thought it would be interesting to list all the keyboard shortcuts that I currently had bound. To do this, I ran the following PowerShell one-liner:

$dte.Commands | ? { $_.Bindings.length -ne 0 -and $_.name.length -ne 0 } | sort Name | select Name, Bindings | % { $b = $_.Name; $_.Bindings | % { $_ -match '(.*)::(.*)' | out-null; new-object psobject -Property @{ Name = $b; Context = $Matches[1]; Key = $Matches[2]} } } | sort Context, Name, Key | select Context, Name, Key | ft -AutoSize
Of course, if you want to grab this information and format it for better printing, just replace the last step in the pipeline (ft -AutoSize) with ConvertTo-Csv | clip. This will copy all your shortcuts into the clipboard in CSV format, and you can paste them into Excel for better formatting and printing.

Wednesday, January 4, 2012

Requiring administrative access in Visual Studio 2010 C++ projects

In previous versions of Visual Studio, making any changes to the application manifest file for a C++ application required you to code one up manually, removing the advantages of the built-in manifest generation tool. However, in Visual Studio 2010, common tasks such as setting the UAC execution level are now baked into the project settings.

Below is a screenshot of how to set the execution level (requestedExecutionLevel) or UI protection (uiAccess) settings in the manifest using the project settings.


Tuesday, December 28, 2010

WiX 3.5 in Visual Studio 2010 undefined preprocessor variable when solution folders used

I ran into a problem with the WiX 3.5 installer in Visual Studio 2010 when trying to add a reference to a project in a solution folder. The folder structure looked like this:

  • Client
    • WiXProject
      • References
        • Common _Common\Common_
  • Common
    • Common
The installer built fine when I set Harvest to True, but, since I wanted more control over the installer, I wanted to manually edit the wxs file like this:

<file diskid="1" id="Common" name="$(var.Common.TargetFileName)" source="$(var.Common.TargetPath)">

Unfortunately, Votive (the WiX integration with Visual Studio) does not seem to correctly handle references to projects in solution folders. The workaround I used was to unload the WiX project and edit the line as highlighted:

    
      Common _Common\Common_
      {89f6f5f2-5aeb-4501-9f4f-9073d7dedd4c}
      True
      True
      Binaries;Content;Satellites
      INSTALLLOCATION
    

    
      Common
      {89f6f5f2-5aeb-4501-9f4f-9073d7dedd4c}
      True
      True
      Binaries;Content;Satellites
      INSTALLLOCATION
    

When I reloaded the project, my wxs file worked as originally configured because the variable var.Common.TargetFileName was now defined.

Wednesday, October 13, 2010

Setting up code analysis in Visual Studio

Code Analysis Rule Files

Copy these files to an appropriate location in the source tree:
For test code:
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Test Code" Description="Rules for unit and integration test assemblies." ToolsVersion="11.0">
  <IncludeAll Action="Error" />
  <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
    <Rule Id="CA1004" Action="None" />
    <Rule Id="CA1014" Action="None" />
    <Rule Id="CA1020" Action="None" />
    <Rule Id="CA1024" Action="None" />
    <Rule Id="CA1026" Action="None" />
    <Rule Id="CA1031" Action="None" />
    <Rule Id="CA1062" Action="None" />
    <Rule Id="CA1300" Action="None" />
    <Rule Id="CA1303" Action="None" />
    <Rule Id="CA1502" Action="None" />
    <Rule Id="CA1506" Action="None" />
    <Rule Id="CA1702" Action="None" />
    <Rule Id="CA1707" Action="None" />
    <Rule Id="CA1709" Action="None" />
    <Rule Id="CA1822" Action="None" />
    <Rule Id="CA2204" Action="None" />
    <Rule Id="CA2210" Action="None" />
  </Rules>
</RuleSet>
For production code:
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Production Code" Description="Rules for production code." ToolsVersion="11.0">
  <IncludeAll Action="Error" />
  <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
    <Rule Id="CA1004" Action="None" />
    <Rule Id="CA1020" Action="None" />
    <Rule Id="CA1024" Action="None" />
    <Rule Id="CA1026" Action="None" />
    <Rule Id="CA1303" Action="None" />
    <Rule Id="CA1502" Action="None" />
    <Rule Id="CA1506" Action="None" />
    <Rule Id="CA1822" Action="None" />
    <Rule Id="CA2204" Action="None" />
    <Rule Id="CA2210" Action="None" />
  </Rules>
</RuleSet>

Language

I am not aware of a GUI that allows setting the language for Visual Studio code analysis.

To do this manually, add the following line to your .csproj files under the main <PropertyGroup> tag:

<CodeAnalysisCulture>en-NZ</CodeAnalysisCulture>

This setting is obviously for New Zealand English. Consult this table for the appropriate locale ID.

Custom Dictionary

Set up a custom dictionary file called CustomDictionary.xml and add a link to it in one of your projects. Set the build action (in the properties page) to CodeAnalysisDictionary. To duplicate this into other projects in the same solution, just drag into each of those solutions. I use the following as a default:
<Dictionary>
  <Words>
    <Unrecognized>
      <Word></Word>
    </Unrecognized>
    <Recognized>
      <Word>Mvc</Word>
      <Word>Serialise</Word>
      <Word>Serialisation</Word>
      <Word>Serialiser</Word>
      <Word>Deserialise</Word>
      <Word>Deserialised</Word>
      <Word>Deserialisation</Word>
      <Word>Nesse</Word>  <!--From FitNesse-->
    </Recognized>
    <Deprecated>
      <Term PreferredAlternate=""></Term>
    </Deprecated>
    <Compound>
      <Term CompoundAlternate=""></Term>
    </Compound>
    <DiscreteExceptions>
      <Term></Term>
    </DiscreteExceptions>
  </Words>
  <Acronyms>
  </Acronyms>
</Dictionary>

Project Configuration

In each project's property pages under the Code Analysis tab, set the rule set to either the test or production set, and enable the code analysis for release builds only. Running code analysis slows down the build and may not be suitable for debug builds.

TFS

In your continuous integration build definition, set code analysis to Always.

Tuesday, July 13, 2010

Relative paths in .testsettings file

To configure relative code coverage paths in the .testsettings file, you will need to manually edit the xml file. First configure your coverage DLLs using the UI, then open the .testsettings file in a text editor, or in VS using the VS text editor.

Edit the absolute paths to be relative to the location of your testsettings file, and use the %outdir% macro instead of bin\Debug:
..\..\Utilities\%outdir%\Utilities.dll