ChatGPT解决这个技术问题 Extra ChatGPT

Compare two files in Visual Studio

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

Just as comment for further use: If you are working with vs2015, use this link: visualstudio.com/en-us/docs/tfvc/compare-files
Note this is still applicable for VS 2017. Hence, I have provided an answer how to deal with this issue comfortably.
None of the answers can compare two files already opened in Visual Studio.
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.

P
Peter Mortensen

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:

https://i.stack.imgur.com/cQF8D.png


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.
Why is this feature hidden from the VS UI ? This is the easiest and effective way of comparing two files...
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
such a shame VS doesn't have a menu item for this very basic functionality. many IDE's/ editors do.
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.
M
Matt

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:

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

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:

Usage of the diff window

Visual Studio's Diff parameter

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


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.
@DivTiwari - Thank you, glad to hear that!
That was superb and worked like a charm. Thanks a lot mate
Nice! up-voting this..just love the 'hackish' nature of this solution. Thank your for sharing.
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.
P
Peter Mortensen

You can try the VSCommands extension from Visual Studio Gallery.

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

https://i.stack.imgur.com/Yg6yA.png

https://i.stack.imgur.com/7KOPE.png


I installed the extensions, but this option isn't available for me.
VSCommands 14 add neither of the items on the screenshot in VS 2015 Update 1 Community
Broken Link for latest release
P
Peter Mortensen

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

Tools.DiffFiles File1 File2

This is the same as another answer
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.
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?
@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.
P
Peter Mortensen

Visual Studio extension: File Differ by Mads Kristensen

https://i.stack.imgur.com/oF5kj.png


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.
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.
P
Peter Mortensen

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


Also able to diff folders - very powerful
The only disadvantage of WinMerge is its limited ablitiy to recognize changes (such as whitespace, lines divided into more lines, etc)
The link is broken: "Hmm. We’re having trouble finding that site. We can’t connect to the server at blog.paulbouwer.com."
@user586399 It looks like that problem has been solver in recent version. By the way, this is a lovely solution still available as 2021.
P
Peter Mortensen

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


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)
Visual Studio Comparison Tools is available for 2013, visualstudiogallery.msdn.microsoft.com/…
P
Peter Mortensen

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

https://i.stack.imgur.com/RHC2E.png

https://i.stack.imgur.com/RgIQc.png

You can install it from here:

https://i.stack.imgur.com/O6wFl.png

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

https://i.stack.imgur.com/EHGsC.png


Unfortunately, this doesn't work when trying to compare two files without solution (for example, when you open VS2017 in folder mode: Open - Folder)
What I was looking for, didn't think of an extension. Excellent!
P
Peter Mortensen

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:

https://i.stack.imgur.com/I40Qx.jpg

It's also available as a separate extension.


I don't see Diff selected files or Compile Markdown to HTML in Web Essential - VS 2015
I have installed new version for VS 2015 but I am not able to see Diff selected files option why ?
@stom install the extension I mentioned.
P
Peter Mortensen

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


Thanks worked perfectly for me. c:\temp>"%VS120COMNTOOLS%..\IDE\vsdiffmerge.exe" 1256.txt 1256_PROD_Original.txt
P
Peter Mortensen

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

Open the Command Window by hotkeys Ctrl + W, A or by menu View → Other Windows → Command Window Enter command Tools.DiffFiles "FirstFile.cs" "SecondFile.cs"


P
Peter Mortensen

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

P
Peter Mortensen

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


Can you add a reference to it? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)
P
Peter Mortensen

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

https://i.stack.imgur.com/un1vj.jpg

https://i.stack.imgur.com/gCSk9.jpg

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

https://i.stack.imgur.com/nNo6y.jpg

Step 3: Merge the files

https://i.stack.imgur.com/z97Bm.jpg

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.


A
Allan Ridolfi

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

https://i.stack.imgur.com/yQega.png

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

https://i.stack.imgur.com/NreLP.png


English version please
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.
M
MARI Mathieu

With Visual studio code its simple.
Create the bat file files_compare.bat with the code:

@echo off
setlocal
code --diff %1 %2

And to use it you just have to drag and drop the 2 files to compare on the bat file icon


P
Peter Mortensen

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