544

I saw the new comparison tool in Visual Studio 2012 for comparing two files or two versions of a file. I like it. But when I tried to find it I couldn't it, because I don't use TFS.

Is there a way how I can just compare two files with the built-in feature in Visual Studio (but without TFS)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182
  • 2
    Just as comment for further use: If you are working with vs2015, use this link: https://www.visualstudio.com/en-us/docs/tfvc/compare-files – Elnaz Apr 09 '17 at 12:08
  • **Note** this is still applicable for VS 2017. Hence, I have provided an **[answer](https://stackoverflow.com/a/45714611/1016343)** how to deal with this issue comfortably. – Matt Aug 23 '17 at 15:47
  • 2
    None of the answers can compare two files **already** opened in Visual Studio. – Just a HK developer May 08 '18 at 06:39
  • 2
    Please vote for this feature here: https://developercommunity.visualstudio.com/content/idea/619706/is-there-a-way-to-compare-two-files-from-solution.html – Happypig375 Jul 02 '19 at 02:55
  • 1
    In VS2017 and VS2019, even the compare file toolbar is open, don't know how to use it...Using VSCode now, just open the two files and select both and context menu->compare selected. – jw_ Jan 15 '20 at 02:13
  • Easier to use WinMerge, and it's open source. – live-love Aug 26 '20 at 18:38

17 Answers17

959

You can invoke devenv.exe /diff list1.txt list2.txt from the Visual Studio Developer 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:

Using Tools.DiffFiles from the Visual Studio Command window

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vladimir Reshetnikov
  • 11,750
  • 4
  • 30
  • 51
  • 20
    Does this diff tool provide any useful feature like copy to left, copy to right, etc? When I diff working copy with the server version I sometimes wish to undo some of the changes and has to do manual copy pasting. – Samuel Feb 12 '14 at 10:24
  • 152
    Why is this feature hidden from the VS UI ? This is the easiest and effective way of comparing two files... – eka808 Feb 14 '14 at 15:23
  • 8
    If your path has spaces, you can wrap either the entire path or just the part part with spaces in quotes. – pennstatephil Jun 24 '14 at 15:16
  • 89
    For those of you who are new to Visual Studio the Command Window can be opened by going to: View > Other Windows > Command Window (VS Pro 2013) or with CTRL+ALT+A – Cleanshooter Jun 26 '14 at 13:30
  • 45
    such a shame VS doesn't have a menu item for this very basic functionality. many IDE's/ editors do. – dewd Sep 16 '14 at 09:09
  • @Cleanshooter thats full version of VS 2013 only, CommandWindow isn't present in Visual Studio Express 2013 for windows desktop – lxx Oct 03 '14 at 01:44
  • 6
    WOW that's great! I'm using VStudio since years and I never knew there was a diff window! I always took 3rd party tools. Why are they hiding such an important feature? –  Nov 05 '14 at 19:10
  • 2
    This doesn't work in vs 2010 and even if it did it still sucks as an answer . What there's no tool built in for diffing files. What kind of half baked IDE BS is this? I can't hilight 2 files and diff them WTF? – ggb667 Nov 20 '14 at 05:32
  • 1
    Is there a similar way to launch the Merge tool? – inko9nito Jan 07 '15 at 18:18
  • can i ignore case comparison in this? ie captial letter small letter difference should be ignored – Vignesh Subramanian Feb 16 '15 at 10:08
  • 2
    @full_prog_full `devenv.exe` is to be invoked from the system command prompt (`cmd.exe`) or from Windows "Run" dialog (`Win+R`), not from Visual Studio. If Visual Studio is already running, use `Tools.DiffFiles` in its Command window. – Vladimir Reshetnikov Jun 02 '16 at 18:31
  • 3
    »Tools.DiffFiles« doesn’t work for me! VS just states that »The command […] is invalid«. I assume that is a language problem, bacause I have a German version of Visual Studio 2013. Tools is called »Extras« in German (which has nothing todo with tools at all!). But »Extras.DiffFiles« also not works. Has anyone an idea what the German version of this is, or does anyone know a different reason, it doesn’t work for me? – Martini Bianco Sep 13 '16 at 09:18
  • Tools.DiffFiles is total useless. It makes a temporary copy of the compared to file, and even worse the temp copy will have the default encoding. – apr Sep 20 '16 at 13:34
  • This method also works on VS 2015. Thanks @VladimirReshetnikov ! – David Sep 28 '16 at 18:55
  • 28
    **Note:** In Visual Studio 2015, you can find it here : **View -> Toolbars -> Compare Files**. Simply type "compare" into the quick launch window and you'll get it added. – Matt Dec 05 '16 at 12:49
  • 2
    @Matt How to actually use it? I tried Ctrl+Click to select two tabs, I even tried arranging side-by-side but it's not working... – Sreenikethan I Aug 21 '20 at 19:08
  • 1
    @SreenikethanI As I wrote in the answer, there are 2 ways to do that: start Visual Studio from the command prompt giving filenames as arguments, e.g. `devenv.exe /diff list1.txt list2.txt`, or open Command Window in an already running instance of Visual Studio and use `Tools.DiffFiles` command. I am not aware of any way to invoke diff by selecting tabs. – Vladimir Reshetnikov Aug 22 '20 at 21:13
  • 2
    @VladimirReshetnikov Ah, I see. I had intended to ask about doing it from the GUI itself, but I'd like to apologize for not making myself clear. Thank you! I ended up using the DiffFiles command from the Command Window – Sreenikethan I Aug 23 '20 at 16:01
  • @SreenikethanI - take a look at [the answer I posted](https://stackoverflow.com/a/45714611/1016343). It contains step-by-step instructions. Check it out, and if you still have questions, leave me a message there. – Matt Aug 24 '20 at 07:34
138

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:

Compare two files example

  1. Either you select one file and then select a second file via "Compare with..." in the context menu (left side of the screenshot)

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

  1. 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.

  2. 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*) )

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

    DragDropDemo

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

    Visual Studio View

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:

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

    SendTo

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

    Visual Studio View

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

  1. In the Windows start menu Windows Icon Small, locate the Visual Studio icon Visual Studio Icon Small

  2. Right-click to bring up the context menu. Select MoreOpen 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:"

    VSProperties


Animations created with Screen to gif

Matt
  • 25,467
  • 18
  • 120
  • 187
  • 14
    This is really a very good and elaborated answer, definitely should be voted up. I am from the group who keeps looking for out of the box answer even if answer is marked in a thread so i am absolutely giving it heads up. – Mantra Feb 16 '18 at 17:48
  • 4
    @DivTiwari - Thank you, glad to hear that! – Matt Feb 19 '18 at 11:32
  • Edit: This is the script for VS 2013, thanks again. @echo off setlocal set vspath=C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE start "Compare files" /B /MIN "%vspath%\devenv.exe" /diff %2 %1 First:'%2' Second:'%1' – Mitch3091 May 11 '18 at 15:55
  • 3
    That was superb and worked like a charm. Thanks a lot mate – Sabbir Hassan Mar 15 '19 at 12:05
  • 4
    Nice! up-voting this..just love the 'hackish' nature of this solution. Thank your for sharing. – Leo Gurdian May 15 '19 at 00:05
  • 2
    Starting from Visual Studio 2017, there is a command line tool to find the path to the latest VS installed: `C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe`. parsing its output in a `for` loop will get you the path to Visual Studio. – Tsahi Asher Aug 18 '19 at 10:18
  • @TsahiAsher - Cool hint, didn't know about **vswhere.exe.** I experimented a bit with it and found the following parameters useful: `vswhere.exe -all -legacy -sort -format value -property installationPath` - which just lists the installation paths of all existing instances. You can use that for batch processing easily. – Matt Aug 19 '19 at 07:55
  • Thanks a lot! I am not good at memorization and I hate looking up this command and typing it every now and then. With this method I'd probably search this stuff less :D thanks. However is it really necessary to specify the full filepath of the devenv.exe? I think this code can be improved by turning "%vspath%\devenv.exe" to "devenv.exe" so that it will just work as long as Visual Studio is installed regardless of version or edition. – RoyceBautista Nov 08 '19 at 09:48
  • @RoyceBautista - Glad you that like it. Regarding your question, the batch file doesn't run in the context of the developer console - it runs in the context of the Windows Explorer, which doesn't resolve devenv.exe without a path. So, yes `%vspath` is needed and cannot be omitted. If you want to know how the context is created in the dev console, take a look into`VsDevCmd.bat`, but it should be noted that is rather complex. – Matt Nov 08 '19 at 10:26
  • Very nice, howdy from 2020. – AbraCadaver Mar 05 '20 at 15:20
  • Nice. But one trouble is, the files need to be in same folder – Byju Feb 07 '21 at 13:34
  • @Byju - Yes, unfortunately, that cannot be fixed since the script is using`%1` and `%2` parameters - Windows Explorer provides the path. And how would you drag & drop if the files are in different folders? – Matt Feb 08 '21 at 07:42
  • @Byju - I've tested drag & drop and found that Windows does not allow you to drag & drop from different folders. All files must be in the same folder. So it is a Windows limitation. – Matt Feb 08 '21 at 13:53
  • 1
    The drag-&-drop works like a charm. Thank you! – stackoverblown Jul 25 '23 at 13:19
  • Diff from File Explorer should be stock installed on all versions of MS Windows 10+. If MS doesn't want to include that by default, then it should become a File Explorer shortcut as soon as anyone installs VSCode or Visual Studio Paid. – Shawn Eary Aug 10 '23 at 15:32
  • 1
    @ShawnEary - Yes, that's true. That's clearly missing in Windows "out-of-the-box". – Matt Aug 11 '23 at 10:47
81

You can try the VSCommands extension from Visual Studio Gallery.

The latest release allows you to select two files and compare them:

Enter image description here

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jarek Kardas
  • 8,445
  • 1
  • 31
  • 32
60

(Command Window) (Ctrl + Alt + A):

Tools.DiffFiles File1 File2
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
patrick
  • 16,091
  • 29
  • 100
  • 164
  • This is the same as another answer – niico Jun 06 '18 at 01:08
  • 14
    Maybe - though personally I'd rather use a GUI to select the files than type have to construct two paths in any case - I mean it's not 1984. – niico Jun 06 '18 at 13:23
  • 2
    thank you, this is by far the simplest solution here! @nico I really don't understand your point, are you a programmer (that type in code too) or you just want to use visual studio to compare files? – Dejan Dozet Aug 12 '20 at 12:18
  • @DejanDozet I believe what niico was referring to is the fact that `Tools.DiffFiles` is mentioned (and pictured) in the first part of the accepted answer by Vladimir. – Wayne Ivory Jun 03 '21 at 06:45
55

Visual Studio extension: File Differ by Mads Kristensen

Compare files screenshot

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
spottedmahn
  • 14,823
  • 13
  • 108
  • 178
  • 3
    This answer needs to be much much much higher. This is the only relevant answer to this question. Anyone who comes to this thread is looking for this exactly this and nothing else. I spent hours trying to find this extension because the top two answer are absolutely useless so I assumed this whole page was useless... Since I wasn't familiar with the jargon "diff" and used the keyword "compare" ive been through the gauntlet. – Gappy Hilmore Sep 18 '21 at 02:09
  • 1
    I completely agree with @GappyHilmore. While reverting to an addin, it only takes one minute to download & install and does exactly what everyone is looking for. Simply select two files in the solution explorer and select "Compare Files..." from the context menu. – Bobbie E. Ray Nov 05 '21 at 06:17
  • Did not worked for me. I had two files/namespaces which had classes. There were same classes at different positions which it showed as removed in fact they were there. But I think for formal file compare with single class it may work. – Inshal Irshad Aug 18 '22 at 13:10
25

I have always been a fan of WinMerge which is an open source project. You can plug it into Visual Studio fairly easily.

http://blog.paulbouwer.com/2010/01/31/replace-diffmerge-tool-in-visual-studio-team-system-with-winmerge/

will show you how to do this

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Also able to diff folders - very powerful – Jaans Dec 03 '15 at 11:02
  • 2
    The only disadvantage of WinMerge is its limited ablitiy to recognize changes (such as whitespace, lines divided into more lines, etc) –  May 18 '16 at 07:58
  • The link is broken: *"Hmm. We’re having trouble finding that site. We can’t connect to the server at blog.paulbouwer.com."* – Peter Mortensen Aug 14 '21 at 13:03
  • @user586399 It looks like that problem has been solver in recent version. By the way, this is a lovely solution still available as 2021. – carloswm85 Sep 21 '21 at 13:47
24

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stephan
  • 4,187
  • 1
  • 21
  • 33
  • 1
    NB - this extension is only for VS2012, but you can edit the manifest file to support VS2013 easily enough - see my comment on the Q & A tab of the extension for details. (I wanted this feature, but can't use the free version of VSCommands at work due to it's licence) – James S Jul 01 '14 at 11:30
  • Visual Studio Comparison Tools is available for 2013, http://visualstudiogallery.msdn.microsoft.com/4a139744-7b66-4976-abbd-184323333a0a?SRC=VSIDE – AlignedDev Jul 18 '14 at 16:26
20

For Visual Studio 2019, the File Differ plugin allows to compare files in distinct projects:

Enter image description here

Enter image description here

You can install it from here:

Enter image description here

OLD:

For Visual Studio 2017, install *[FileComparer2017][4]*

The problem is that you can't compare files in different projects, but you can copy the files in the same project to compare...

File difference

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zaha
  • 846
  • 10
  • 21
17

In Visual Studio 2012, 2013, 2015, you can also do it with Web Essentials. Just right click the files and from the context menu → Web EssentialDiff selected files:

Enter image description here

It's also available as a separate extension.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamid Mosalla
  • 3,279
  • 2
  • 28
  • 51
15

If you have Visual Studio installed, you could also call

"%VS110COMNTOOLS%..\IDE\vsdiffmerge.exe" "File1" "File2"

or for Visual Studio 2013:

"%VS120COMNTOOLS%..\IDE\vsdiffmerge.exe" "File1" "File2"

Source: Use Visual Studio as your Diff and Merging Tool for local files

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Thanks worked perfectly for me. c:\temp>"%VS120COMNTOOLS%..\IDE\vsdiffmerge.exe" 1256.txt 1256_PROD_Original.txt – Craig B Mar 16 '15 at 01:20
8

In Visual Studio, the diff can be called using the Command Window and then the Tools.DiffFiles command

  1. Open the Command Window by hotkeys Ctrl + W, A or by menu ViewOther WindowsCommand Window
  2. Enter command Tools.DiffFiles "FirstFile.cs" "SecondFile.cs"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

I had this problem as well. No TFS, but I found the article Associate a file type with a file-comparison tool helpful.

Specifically, step 1b.

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

tf diff /configure
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
billb
  • 3,608
  • 1
  • 32
  • 36
1

When using Visual Studio with a localized language, the commands you write in the CommandWindow must be also localized. For example, my VS2019 is in portuguese so, i have to write this command:

Ferramentas.CompararArquivos

enter image description here

You can show the CommandWindow (Janela de Comando in my case) through the menu View > Other Windows > Command Window or, in my case Exibir > Outras Janelas > Janela de Comando

enter image description here

  • English version please – Ali Karaca Mar 10 '22 at 08:28
  • 2
    Hi @AliKaraca, my comment was made just for warn users on localized versions of VS. As I wrote, in my case, the other answers was not working by just one reason: localization. So i hope to help people that uses localized VS. – Allan Ridolfi Mar 11 '22 at 10:20
1

With Visual Studio 2022 17.7 Preview 1, a new Compare Files option has been added to Solution Explorer.

In response to this highly upvoted ticket in Developer Community, you can now compare files in Solution Explorer

Right click on a file in solution explorer, and select "Select for Compare". Then, right click on the file you wish to compare to and select "Compare with {fileName}":

select for compare and compare with file options in solution explorer

This will open up the a diff comparison tab like so:

diff comparison tab showing compared files

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
1

The Comparison of two files has become very easy now with visual studio 2022 17.7 version.

Now you can easily compare any file in Solution Explorer with other files by either:

  • Right-clicking on a single file, selecting “Compare With…” from the context menu which will bring up File Explorer. Navigating to any file on disk and selecting it for comparison.
  • Multi-selecting two files by holding down the Ctrl button then right-clicking and selecting “Compare Selected” from the context menu.

screenshot from Microsoft

Reference: https://devblogs.microsoft.com/visualstudio/visual-studio-2022-17-7-now-available/#comparing-files

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

An excellent lightweight tool that gets the job done is the File Comparer Visual Studio extension by Akhil Mittal.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
usefulBee
  • 9,250
  • 10
  • 51
  • 89
  • Can you [add](https://stackoverflow.com/posts/40410128/edit) a reference to it? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Aug 14 '21 at 13:20
0

To compare any two files and merge it to one file, here are the following steps you can follow if you have Visual Studio (any version) installed.

Step 1: Open the Visual Studio command prompt. If you do not find the Visual Studio command prompt then choose Visual Studio tools

Start → Visual Studio command prompt

Enter image description here

Enter image description here

Step 2: Enter the command vsdiffmerge.exe

Ignore the switch /m if you need just comparison.

Syntax 1:

vsdiffmerge <file1> <file2> <file1> <outputfile> /t /m

Syntax 2:

vsdiffmerge <basefilename> <CompareFilename> <basefilename> <OutputFilename> /t /m

Example 1:

vsdiffmerge test1.js test2.js test1.js output.js /t /m

Example 2:

vsdiffmerge.exe "C:\Users\livingston\Downloads\wa\wa\Files\pre\Test.js" "C:\Users\livingston\Downloads\wa\wa\Files\Prod\Test.js" "C:\Users\livingston\Downloads\wa\wa\Files\pre\Test.js" "C:\Users\livingston\Downloads\wa\wa\Files\output\samp.js" /t /m

Enter image description here

Step 3: Merge the files

Enter image description here

Please note that if file name does not exist in the location, it will not open the comparer.

Also you can beautify the file before you do the comparison. In Visual Studio, Ctrl + K + D.

There are a lot of beautifier sites available online.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
George
  • 2,842
  • 2
  • 15
  • 11