VisualStudio

From HerzbubeWiki
Jump to navigation Jump to search

This page contains notes about Visual Studio, the Microsoft IDE for writing applications for Windows desktop and mobile devices. Obviously the information on this page is bound to become out of date over time as Visual Studio is a bit of a moving target.


Configuration

This section has some details about how I like to configure Visual Studio (under Tools > Options):

  • Environment
    • General
      • Turn off upper case in menu bar = true
    • Fonts and Colors
      • Font size = Set depending on monitor / resolution. On my Apple monitor at home I like to set this to 16.
    • Startup
      • At startup = Show empty environment
    • Tabs and Windows
      • Preview selected files in Solution Explorer = false
      • Preview selected files in Find Results = false
      • Allow new files to be opened in the preview tab = False
  • Projects and Solutions
    • General
      • Track Active Item in Solution Explorer = False
    • Build and Run
      • Number of parallel project builds = Set to the number of cores in the environment
  • Source Control > Visual Studio Team Foundation Server
    • Get latest version of item on check out = False
      • Setting this to False is very important!!! If this is left to True, the "getlatest" operation will invariably get a version of the file that is inconsistent with the rest of the project or solution
    • Open Source Control Explorer to the most recent folder = true
    • Prompt before check-in from Pending Changes window = True
    • Resolve associated work items on check-in = False
      • I set this to false because I frequently check in several changes for a single work item, so I want to be in control of when the work item is resolved.
  • Debugging > General
    • Step over properties and operators = False
    • Highlight entire source line = True
  • Text editor
    • All Languages
      • General
        • Line numbers = true
      • Tabs
        • Indenting = Smart
        • Tab size = 4
        • Indent size = 4
        • Keep tabs = true
      • C/C++
        • Advanced > Use Forward Slash in #include Auto Complete = True
        • Formatting > General > Automatic indent when I type a tab = True
        • Formatting > Indentation > Indent case labels = True


Unfortunately the text editor tab settings are global, which means that you will get into trouble when you work on different projects with different code formatting guidelines. Because the tab settings are cloud-synchronized, you will have trouble even if you develop on different machines.


Keyboard shortcuts

Configure keyboard shortcuts under "Tools > Options > Environment > Keyboard". Use the "Global" context unless specified otherwise.

  • Ctrl + Shift + B = Build Solution (already the default in VisualStudio 2019)
  • Ctrl + B, Ctrl + B = Build.BuildSelection (the default shortcut is Ctrl + B, and I would actually prefer the default, the problem is that it does not work because there are many chords that begin with Ctrl + B)
  • Ctrl + T = TestExplorer.ShowTestExplorer (by default Ctrl + T is assigned to Edit.CharTranspose (in Text Editor); this function has so little value that it is safe to reassign the shortcut)
  • Compare with latest version
    • Visual Studio 2010: Ctrl + Shift + D = TeamFoundationContextMenus.SourceControlPendingChangesSourceFiles.Compare.TfsContextPendingCheckinsCompareWithLatestVersion
    • Visual Studio 2013: Ctrl + Shift + D = TeamFoundationContextMenus.PendingChangesPageChangestoInclude.TfsContextPendingChangesPageCompareWithWorkspaceVersion
    • With Git: Ctrl + Shift + D = Team.Git.CompareWithUnmodified
    • With TFS: Shift + Enter (already the default in VisualStudio 2019, possibly works in Pending Changes view only)
  • Alt + O = EditorContextMenus.CodeWindow.GoToHeaderFile (in "Text Editor" context)
    • With CodeMaid: CodeMaid.SwitchFile (in "Text Editor" context)
  • Ctrl + Num 1 = View.FindResults1
  • Ctrl + Num 2 = View.FindResults2
  • Pending Changes
    • With Git: Ctrl + Num 3 = View.PendingChanges
    • With TFS: Ctrl + Alt + Num 3 = View.TfsPendingChanges
  • Ctrl + Num 4 = View.Output
  • Ctrl + Num 5 = View.ErrorList
  • View History
    • With Git: Ctrl + Num 6 = Team.Git.ViewHistory
    • With TFS: Ctrl + Alt + Num 6 = File.TfsHistory
  • Annotate
    • With Git: Ctrl + Num 7 = Team.Git.Annotate
    • With TFS: Ctrl + Alt + Num 7 = File.TfsAnnotate
  • Ctrl + Num 8 = View.QuickAccess (available only when the "Quick Access" extension from the "Productivity Power Tools" extension pack is installed)
  • Ctrl + Num 9 = View.SolutionExplorer
  • Ctrl + Num / = Team.GotoWorkItem
  • Ctrl + Num * = TeamFoundationContextMenus.Commands.GoToBuilds
  • Ctrl + Num - = File.TfsContextExplorerMerge
  • Ctrl + , Ctrl + L = Edit.Trimlineends
  • Alt + U = Edit.GoToFile (stolen from the extension DPack)


Useful default keyboard shortcuts:

  • Ctrl + F12 = Edit.GoToDeclaration


Toolbar

  • Create a new toolbar
  • Name = "foo"
  • Add buttons for the following commands:
    • View > TfsSourceControlExplorer
    • File > TfsWorkspaces


Test settings

  • Test > Windows > Test Explorer > Run Tests After Build (unfortunately you have to manually switch to the Test Explorer view to see if any tests failed; with a keyboard shortcut this switch can be made so quickly that, in time, the check hopefully becomes second nature)


Configure alternative Compare/Merge tools

It's possible to replace Visual Studio's default Diff/Merge tool (diffmerge.exe) with alternative external tools. My personal preference for the Diff tool is WinMerge, but I haven't found a viable alternative for the Merge Tool.


General:


Use WinMerge as compare tool

  • Extension = .*
  • Operation = Compare
  • Command = C:\Program Files\WinMerge\WinMergeU.exe
  • Arguments = /x /e /u /wl /dl %6 /dr %7 %1 %2
    • /x = Display an info dialog when identical files are compared, then close WinMerge automatically
    • /e = Close with Escape
    • /u = Don't add files to MRU list
    • /wl = Left side is read-only
    • /dl %6, /dr %7 = Displays %6 and %7 as descriptions on the left/right pane
  • Note: "Ignore whitespace" cannot be specified as a command line argument, the function must be enabled in the WinMerge options (Edit > Options > Compare > Whitespace = Ignore change)


The default compare tool (diffmerge.exe), but with "ignore whitespace"

  • Extension = .*
  • Operation = Compare
  • Command = C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\diffmerge.exe
  • Arguments = %1 %2 %6 %7 %5 /ignorespace


Use WinMerge as merge tool (not recommended)

  • Extension = .*
  • Operation = Merge
  • Command = C:\Program Files (x86)\WinMerge\WinMergeU.exe
  • Arguments = /x /e /u /wl /dl %6 /dr %7 %1 %2 %4


Extensions

  • Productivity Power Tools
    • This is actually a collection of extensions. After the main extension is installed, the individual extensions can be enabled/disabled here: Tools > Options > Productivity Power Tools
    • Over time many of the most useful functions have been integrated into Visual Studio. Useful functions that are still part of the extension: Fix Mixed Tabs, Edit Project File (opens the .csproj or .vcxproj in the text editor).
  • DPack
    • The single interesting feature of this extension is that with shortcut Alt + U you open a small window that lets you quickly open any file in the solution simply by entering its name, or a partial name. You don't have to navigate to the file via Solution Explorer.
    • Since Visual Studio 2013 the Solution Explorer also has a search function, but I find this pretty unusable, so I still use this extension.
    • Configuration: Solution Backup > General > Backup the entire solution = false
  • Editor Guidelines
    • There may be several extensions of the same name, I am using the one by Paul Harrington.
    • I use this to show a vertical guideline in the text editor at column 80, so that I can immediately see when the line I'm writing gets too long.
    • To set a guideline, place the cursor in the editor in the desired column, then open the context menu and select the appropriate entry.
  • AnkhSVN
    • If AnkhSVN should be the default Source Control system
      • Go to "Tools > Options > Source Control > Plug-in Selection"
      • Select AnkhSVN
    • If AnkhSVN should only be used on a per-project basis
      • Select "File > Subversion > Change Source Control"


Team Foundation Server Power Tools

Search for this in the dialog where you manage Visual Studio's extensions. Downloading this does not install a Visual Studio extension, though, but an installer for a standalone software package.

This software package no longer exists since Visual Studio 1017. With the exception of the TFS shell integration in Windows Explorer all features are now part of Visual Studio.


Git and Visual Studio

General information about Git

For the general usage of Git, see this wiki page.


Git integration in Visual Studio

In Visual Studio 2012 you have to install the extension "Visual Studio Tools for Git": https://visualstudiogallery.msdn.microsoft.com/abafc7d6-dcaa-40f4-8a5e-d6724bdb980c

Since Visual Studio 2013 this extension is already integrated in Visual Studio.


Important: As mentioned in this StackOverflow answer there exist two external solutions besides the Git SCM provider that is integrated into Visual Studio: "Git Extensions" and "Git Source Control Provider". The following sections on this wiki page do not discuss these external solutions. This is especially important for when you research solutions to problems with the underlying Git configuration, e.g. when you have to fiddle with .gitconfig. The external solutions and the integrated Git SCM provider sometimes have subtle differences in this regard.


Paths

The global .gitconfig file is located here:

C:\Users\<username>\.gitconfig

The base folder that is used by default to store repositories is located here:

C:\Users\<username>\Source\Repos


Keyboard shortcuts

Keyboard shortcuts are configured under "Tools > Options > Environment > Keyboard". Unless noted otherwise you can use the "Global" context. Unfortunately I have not found a context that is specific to Git, which would prevent conflicts with the shortcuts defined for the TFS SCM provider. Because of this, I have chosen the following shortcuts so that they are similar to the TFS shortcuts, but mostly use an additional Alt key.

  • Ctrl + Alt + D = Team.Git.CompareWithUnmodified
  • Ctrl + Alt + Num 3 = Team.Git.GoToGitChanges
  • Ctrl + Alt + Num 6 = Team.Git.ViewHistory
  • Ctrl + Alt + Num 9 = Team.Git.Annotate


Potentially useful but not yet customized shortcuts:

  • Ctrl + 0, O = Team.Git.GoToGitCommits
  • Ctrl + 0, N = Team.Git.GoToGitBranches


Clone a repository

  • In the menu select the enry "Team > Connect to Team Foundation Server..."
  • In the window that now opens there is a section at the bottom titled "Local Git Repositories"
  • In this section, click the link "Clone"
  • In the first field you enter the clone URL. You get the clone URL at the upstream source from where you want to clone the repository. Examples:
  • In the second field you enter the local path where you want to store the cloned repository. Usually the default value should be OK. Examples:
    • C:\Users\<username>\Source\Repos\Orc.Wizard
    • C:\Users\<username>\Source\Repos\My First Project


Configure user information

After cloning the first repository you should immediately configure your user information, so that commits will be created with the correct information.

  • Select "Team > Connect to Team Foundation Server > Git Repository"
  • Click the button "Settings"
  • Click the link "Git Settings"
  • User Name = Patrick Näf
  • Email Address = herzbube@herzbube.ch

These settings are now valid for all repositories.

Note: As is usual for Git, the information is stored in the global .gitconfig file. This means that you can also do the configuration via command line (git config ...) or in a text editor.


Remove local repository

  • Select "Team > Connect to Team Foundation Server"
  • Righ-click the repository to remove
  • Select "Remove" from the context menu
  • If the context menu entry is disabled you have to switch to a different repository first because, apparently, it is not possible to remove the project that is currently active.


Note: The repository still exists on the hard disk, it is just not visible in Visual Studio anymore. If you want to completely delete the repository you have to do so in Windows Explorer. Usually repositories are located below your home directory, e.g. C:\Users\<username>\Source\Repos.


Install and use command line tools

Visual Studio offers to install the Git command line tools:

  • Select "Team > Connect to Team Foundation Server"
  • Double-click one of the local repositories
  • In the upper part of the view you should now see the following offer: "Install 3rd-party Git command prompt tools"
  • Click the "Install" link
  • This downloads an installer that you have to execute manually (msysgitvs.exe)
  • The setup process is pretty straightforward, so no further explanations here


To use Git on the command line:

  • Select "Team > Connect to Team Foundation Server"
  • Double-click one of the local repositories
  • Click the "Changes" button at the top
  • Open the "Actions" dropdown menu
  • Select the menu entry "Open Command Prompt"


A DOS command prompt now opens, with the current working directory set to the project repository path. Here you can use the usual Git commands.


Configure WinMerge as Diff tool for Git

Visual Studio does not offer a GUI for configuring a non-default Diff tool for Git. You have to configure this manually by editing the global .gitconfig file.

[diff]
	tool = winmerge

[difftool "winmerge"]
	cmd = \"C:/Program Files (x86)/WinMerge/WinMergeU.exe\" \"$LOCAL\" \"$REMOTE\"

Important:

  • You have to reproduce the combination of backslashes and quotation marks exactly as given here, otherwise paths that contain spaces will not work.
  • You have to use the key "tool" in the "[diff]" section - not "guitool" (many guides on the web erroneously say it's "guitool")


GitHub PR handling

PR = Pull Request


Create a pull request:

  • Create a new local branch: pr/branch-name
    • Obviously the branch name should be related to the purpose of the PR
    • The PR branch must be split off whichever branch you want the changes to be integrated in. Typical branches: master, develop
  • Switch to the PR branch
  • Implement the changes
  • Publish the PR branch to GitHub (= push the branch)
  • Navigate to GitHub in the browser and create the PR
  • GitHub docs: https://help.github.com/articles/creating-a-pull-request/


Manage PR:

  • The upstream maintainer will review the PR. Be prepared to answer questions and/or make additional modifications
  • You can simply implement further changes locally, then push them - as long as the changes occur on the PR branch, GitHub is smart enough to automatically add the new commits to the PR


Once the upstream maintainer has accepted the PR, you can delete the PR branch. You also need to sync your cloned repo with upstream. You can't do this from the GitHub web interface, instead you have to use the command line:

cd /path/to/local-repo
git fetch upstream
git checkout master         # or whatever branch was the base for the PR branch
git merge upstream/master   # ditto
git push


Visual C++ projects

See the VC++ page.


Microsoft.NET projects

See the Microsoft.NET page.


Team Foundation Server

See the TFS page.


Resharper

Useful keyboard shortcuts:

  • Shift + Alt + L = ReShareper.ReSharper_LocateInSolutionOrAssemblyExplorer
    • This seems to be pretty much the same as SolutionExplorer.SyncWithActiveDocument, which by default is bound to Ctrl + ', Ctrl + S


Obtaining a customer system/disk image

Before creating the image

Before the customer creates the system/disk image she should create a temporary local user account - preferrably with administrator rights. The customer should then communicate the temporary account's password to you. The customer should also verify that the problem can be reproduced with the temporary account. After she has created the system/disk image the customer can delete the temporary account.


In adddition you need the following information from the customer:

  • BIOS type (EFI, UEFI, ...)
  • Operating system version (Windows 7, Windows 8, ...)
  • Operating system platform (32-bit, 64-bit)
  • Hard disk size


Create a system image

Since Windows Vista there is a function integrated in Windows that allows the user to easily create a system image:

  • Launch the control panel
  • Select "Backup and restore" (German: "Sichern und Wiederherstellen")
  • Select "Create a system image" (German: "Systemabbild erstellen")
  • Save the system image to an external hard disk (or whatever is convenient to transfer the image to you)


Create a disk image

Instead of a system image it is also possible to create the image of hard disk. The simplest method is to use the tool disk2vhd. This tool is part of the well-known Sysinternals program suite, but it's also possible to download it standalone: http://technet.microsoft.com/en-us/sysinternals/ee656415.aspx.

The tool is very simple to use, and it's sufficiently clever so that you can create the disk image while the system is running! Usually you can simply run the tool with the default settings. The only thing to make sure is that the target disk image format is .vhd, not .vhdx, because converting to .vmdk (see next section) is more difficult for .vhdx. The setting with which you choose the target format is a checkbox labelled "Use Vhdx" in the tool's main window - you have to de-select this checkbox.

.vhdx is a newer format than .vhd: http://technet.microsoft.com/en-us/library/hh831446.aspx


Converting the disk image

Currently I know of 2 tools that can convert a disk image from .vhd or .vhdx into the .vmdk format. The .vmdk format is necessary so that the image can be used in VMware.

  • WinImage: Can only convert .vhd. This is shareware that probably stops working after 30 days.
  • StarWind Image Converter. Can convert both .vhd and .vhdx. For converting from .vhdx the tool must be started on Windows 8. This is freeware, but you have to register to be able to download it.


Use .vhd/.vhdx without conversion

Disk images using the format .vhd or .vhdx apparently can be used without conversion like this:

  • VirtualBox: An alternative virtualizing software. This is free / open source and can directly use .vhd or .vhdx images.
  • Windows 8 and newer apparently can be configured so that Hyper-V virtual machines can be executed natively. Because .vhd and .vhdx are the native Hyper-V Disk Image Format, it might be possible to use customer disk images without conversion. I have never done this and also have not yet researched this option.


Make a disk image bootable

If the disk image is not bootable, for any reason, it can be made bootable again like this:

  • Mount the ISO image of an original Windows CD
  • Launch the virtual machine that uses the problematic disk image
  • The virtual machine should now boot from the Windows CD. After selecting the installation language you should be offered various repair options. Select the one that fixes problems when booting Windows. The options are labelled differently for each Windows version, so no details here.
  • If that does not help, you can select the option that opens a console (cmd.exe) and then enter these commands:
Bootrec.exe /FixMbr
Bootrec.exe /FixBoot


Restore a system image

If the customer has created a system image, it can be restored like this:

  • Create a new virtual machine in VMware. The virtual machine must have the same properties that the customer gave you (Windows version and platform, disk size, BBIOS type).
  • Configure th virtual machine so that the ISO image of the matching original Windows CS is mounted
  • Boot from the CD
  • Use the recovery optios to restore the system image