visual-studio 2017 - Compare two files in Visual Studio




8 Answers

You can invoke devenv.exe /diff list1.txt list2.txt from the command prompt or, if a Visual Studio instance is already running, you can type Tools.DiffFiles in the Command window, with a handy file name completion:

and folders

I saw new comparsion tool in VS 2012 for comparing two files or two versions of file. I like it. But when I tried to find it I can't because I don't use TFS. Is there a way how can I just compare two files with builtin feature in VS but without TFS?




Inspired by the accepted 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.

File compare using drag & drop

Preparation:

1. Create a new batch file using your favorite text editor. Type the following:

@echo off
setlocal
set vspath=C:\Program Files (x86)\Microsoft Visual Studio\2017\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 2nd file as first parameter, then the 1st file as second parameter.

  1. Save this code as VS_FileCompare.cmd to use it, modify vspath if required to match the location of devenv.exe (depending on the Visual Studio version you're currently using) *)

  2. 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:

  1. Open the Windows explorer via Win + E
  2. Select two files to compare in the explorer
  3. Drag and drop them as shown in the animation below:

  4. 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 2 files at a time.


Alternative way: SendTo context menu

Here's an alternative how you can use the batch file. It allows to use the context menu's Send To folder to compare the files.

Preparation:

  1. Place the shortcut into the SendTo folder. Open the Windows explorer via Win + E
  2. 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:

  1. Open the Windows explorer via Win + E
  2. Select two files to compare in the explorer
  3. Assuming the shortcut 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:

  4. After a few seconds (depending on the launch time of Visual Studio), the results will be shown in Visual Studio:


MSDN References:
- Usage of diff window
- Visual Studio's Diff parameter


*) 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):

  1. In the Windows start menu , locate the Visual Studio icon
  2. Right-click to bring up the context menu. Select More > Open File Location.
    Windows Explorer opens with the Visual Studio shortcut highlighted.
  3. Right-Click on the Visual Studio and select Properties
  4. In the properties dialog, you can find the path in "Target:"



There is also a Visual Studio extension called CompareFiles, which does nothing else but adding the "Compare Files" entry to the solution explorer context menu. It invokes the built-in Visual Studio diff tool.

Just in case that someone (like me) doesn't want to install an all-in-one extension like VSCommands...







I had this problem as well. No TFS, but I found this article helpful.

Specifically, step 1b.

Open a Visual Studio command prompt and navigate to the Common7/IDE folder and type

tf diff /configure



In Visual Studio Code you can:

  • Go to the Explorer
  • Right click the first file you want to compare
  • Select Select for compare
  • Right click on the second file you want to compare
  • Select Compare with '[NAME OF THE PREVIOUSLY SELECTED FILE]'



Visual Studio code is great for this - open a folder, right click both files and compare.




I believe this to be one of the better extension for Visual Studio 2012, it's called Code Compare and can be found here.




Related

visual-studio file compare comparison diff