I) Visual Studio 17.7.0 Preview 5
In the latest preview of Visual Studio (Version 17.7.0 Preview 5.0), you can now compare files using the context menu in the Solution Explorer.
Example:
Either you select one file and then select a second file via "Compare with..." in the context menu (left side of the screenshot)
Or you select two files and then use "compare selected" in the context menu (right side of the screenshot)
(The 3rd option - mark it as "select for compare" also exists, but I doubt you would use it very often)
This is a great improvement!
However, in case you want to trigger file compare from outside of Visual Studio (drag & drop or via Windows file explorer's context menu), you can read the original answer I posted (below), which is now an alternative option you have (or the only option, if you're using older versions of Visual Studio).
Inspired by Vladimir Reshetnikov's answer above, I found a very comfortable way how you can instantly compare two files with Visual Studio by using drag and drop or via the "Send To" context menu. It only requires a little preparation which you need to do once and then it is useful like a Swiss army knife.
Visual Studio already has everything you need, and there are only some configuration steps required to make this working:
II) File compare using drag & drop
Preparation:
Create a new batch file using your favorite text editor. Type the following:
@echo off
setlocal
set vspath=C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE
start "Compare files" /B /MIN "%vspath%\devenv.exe" /diff %2 %1 First:'%2' Second:'%1'
You might notice that I have reversed the %1
and %2
parameters in the batch. This is because I noticed that the file explorer passes the second file as the first parameter, and then the first file as the second parameter.
Save this code as VS_FileCompare.cmd
to use it, and modify vspath
if required to match the location of devenv.exe
(depending on the Visual Studio version you're currently using, see footnote*) )
Either create a shortcut named "File Compare" for VS_FileCompare.cmd
and place it on the desktop (as used in the animation below), so it is always available to drag & drop files onto it or directly place the batch file on the desktop. That's all!
Usage:
Open the Windows explorer via Win + E
Select two files to compare in the explorer
Drag and drop them as shown in the animation below:
After a few seconds (depending on the launch time of Visual Studio), the results will be shown in Visual Studio:
Note: It does not harm if Visual Studio is already open. In this case it will just open up a new window within the running instance of Visual Studio. So you can compare multiple file pairs, but please ensure you have selected only two files at a time.
III) Alternative way: SendTo context menu
Here's an alternative how you can use the batch file VS_FileCompare.cmd
mentioned in the section above. It allows to use the context menu's Send To folder to compare the files.
Preparation:
- Create a shortcut "Compare2Files VS" for the batch file
VS_FileCompare.cmd
and copy it into the SendTo folder. Open the Windows explorer via Win + E
- Open the SendTo folder by entering
shell:sendto
into the file explorer's address bar (as described here). Then, put the prepared shortcut into this folder.
Usage:
Open the Windows explorer via Win + E
Select two files to compare in the explorer
Assuming the shortcut for the batch file VS_FileCompare.cmd
is named "Compare2Files VS", you can select the two files, right-click and select Send To → Compare2Files VS to invoke the compare as shown below:
After a few seconds (depending on the launch time of Visual Studio), the results will be shown in Visual Studio:
HINT: If you like the SendTo folder approach, there is more you can do - for example you can open a command shell directly via SendTo and it starts with the right path (the path where the selected file resides). Look here to find out how to do that. You can even combine it with the script to gain elevated rights, with only a little extra effort.
MSDN References:
*) Footnote: Because vsPath
(the path to DEVENV.exe
) differs depending on your version of Visual Studio, I am describing how you can find it out (Windows 10):
In the Windows start menu , locate the Visual Studio icon
Right-click to bring up the context menu. Select More → Open File Location.
Windows Explorer opens with the Visual Studio shortcut highlighted.
Right-Click on the Visual Studio and select Properties
In the properties dialog, you can find the path in "Target:"
Animations created with Screen to gif