在 Visual Studio 中比较两个文件

Compare two files in Visual Studio

提问人:Libor Zapletal 提问时间:12/7/2012 最后编辑:Peter MortensenLibor Zapletal 更新时间:8/16/2023 访问量:494805

问:

我在 Visual Studio 2012 中看到了新的比较工具,用于比较两个文件或文件的两个版本。我喜欢。但是当我试图找到它时,我无法找到它,因为我不使用 TFS

有没有办法使用 Visual Studio 中的内置功能(但没有 TFS)比较两个文件?

visual-studio 文件 比较 比较 差异

评论

2赞 Elnaz 4/9/2017
作为进一步使用的评论:如果您正在使用 vs2015,请使用此链接:visualstudio.com/en-us/docs/tfvc/compare-files
0赞 Matt 8/23/2017
请注意,这仍然适用于 VS 2017。因此,我提供了一个如何舒适地处理这个问题的答案
2赞 Just a HK developer 5/8/2018
任何答案都无法比较在 Visual Studio 中打开的两个文件。
2赞 Happypig375 7/2/2019
请在此处为此功能投票:developercommunity.visualstudio.com/content/idea/619706/...
1赞 jw_ 1/15/2020
在 VS2017 和 VS2019 中,连比较文件工具栏都打开了,不知道怎么用......现在使用 VSCode,只需打开两个文件并同时选择两个文件,然后选择上下文菜单->比较。

答:

25赞 user1877337 12/7/2012 #1

我一直是 WinMerge 的粉丝,这是一个开源项目。可以很容易地将其插入 Visual Studio。

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

将向您展示如何执行此操作

评论

0赞 Jaans 12/3/2015
还能够比较文件夹 - 非常强大
2赞 5/18/2016
WinMerge 的唯一缺点是它识别更改的能力有限(例如空格、分成更多行的行等)
0赞 Peter Mortensen 8/14/2021
链接断开:“嗯。我们无法找到该站点。我们无法连接到 blog.paulbouwer.com 的服务器。
0赞 carloswm85 9/21/2021
@user586399 看起来这个问题在最近的版本中已经解决了。顺便说一句,这是一个可爱的解决方案,在 2021 年仍然可用。
81赞 Jarek Kardas 12/7/2012 #2

可以从 Visual Studio 库试用 VSCommands 扩展

最新版本允许您选择两个文件并进行比较:

Enter image description here

Enter image description here

评论

1赞 PeterX 1/7/2016
我安装了扩展,但此选项不适用于我。
3赞 Mark Toman 2/16/2016
VSCommands 14 在 VS 2015 Update 1 社区中的屏幕截图中均未添加任何项目
0赞 2/23/2022
最新版本的断开链接
2赞 billb 5/5/2013 #3

我也有这个问题。没有 TFS,但我发现将文件类型与文件比较工具关联一文很有帮助。

具体来说,步骤 1b。

打开 Visual Studio 命令提示符并导航到 Common7/IDE 文件夹,然后键入

tf diff /configure
24赞 Stephan 6/12/2013 #4

还有一个名为 CompareFiles 的 Visual Studio 扩展,它除了将“比较文件”条目添加到“解决方案资源管理器”上下文菜单之外,不执行任何其他操作。它调用内置的 Visual Studio 差异工具。

以防万一有人(像我一样)不想安装像 VSCommands 这样的多合一扩展......

评论

1赞 James S 7/1/2014
注意 - 此扩展仅适用于 VS2012,但您可以编辑清单文件以轻松支持 VS2013 - 有关详细信息,请参阅我在扩展的 Q &A 选项卡上的评论。(我想要这个功能,但由于它的许可证,不能在工作中使用 VSCommands 的免费版本)
0赞 AlignedDev 7/19/2014
Visual Studio 比较工具可用于 2013 年,visualstudiogallery.msdn.microsoft.com/...
959赞 Vladimir Reshetnikov 7/16/2013 #5

可以从 Visual Studio 开发人员命令提示符调用,或者,如果 Visual Studio 实例已在运行,则可以在命令窗口中键入,并附带方便的文件名完成:devenv.exe /diff list1.txt list2.txtTools.DiffFiles

Using Tools.DiffFiles from the Visual Studio Command window

评论

20赞 Samuel 2/12/2014
这个差异工具是否提供任何有用的功能,如向左复制、向右复制等?当我将工作副本与服务器版本进行比较时,我有时希望撤消一些更改,并且必须进行手动复制粘贴。
153赞 eka808 2/14/2014
为什么此功能在 VS UI 中隐藏?这是比较两个文件的最简单有效的方法...
89赞 Cleanshooter 6/26/2014
对于不熟悉 Visual Studio 的用户,可以通过转到以下位置打开命令窗口:>查看其他 Windows >命令窗口 (VS Pro 2013) 或使用 CTRL+ALT+A
45赞 dewd 9/16/2014
真是太可惜了,VS 没有这个非常基本的功能的菜单项。许多 IDE/编辑器都这样做。
28赞 Matt 12/5/2016
注意:在 Visual Studio 2015 中,可以在此处找到它:查看 -> 工具栏 ->比较文件。只需在快速启动窗口中输入“比较”,您就会添加它。
15赞 Chandramouleswaran Ravichandra 11/1/2014 #6

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

评论

0赞 Craig B 3/16/2015
Thanks worked perfectly for me. c:\temp>"%VS120COMNTOOLS%..\IDE\vsdiffmerge.exe" 1256.txt 1256_PROD_Original.txt
17赞 Hamid Mosalla 9/17/2015 #7

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.

评论

0赞 usefulBee 11/4/2016
I don't see Diff selected files or Compile Markdown to HTML in Web Essential - VS 2015
0赞 Shaiju T 8/25/2017
I have installed new version for but I am not able to see option why ?VS 2015Diff selected files
0赞 Hamid Mosalla 10/1/2017
@stom install the extension I mentioned.
0赞 usefulBee 11/4/2016 #8

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

评论

0赞 Peter Mortensen 8/14/2021
Can you add a reference to it? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)
137赞 Matt 8/16/2017 #9

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 and 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.%1%2

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

  3. Either create a shortcut named "File Compare" for 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!VS_FileCompare.cmd

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 (the path to ) differs depending on your version of Visual Studio, I am describing how you can find it out (Windows 10):vsPathDEVENV.exe

  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

评论

14赞 Mantra 2/17/2018
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.
4赞 Matt 2/19/2018
@DivTiwari - Thank you, glad to hear that!
3赞 Sabbir Hassan 3/15/2019
That was superb and worked like a charm. Thanks a lot mate
4赞 Leo Gurdian 5/15/2019
Nice! up-voting this..just love the 'hackish' nature of this solution. Thank your for sharing.
2赞 Tsahi Asher 8/18/2019
Starting from Visual Studio 2017, there is a command line tool to find the path to the latest VS installed: . parsing its output in a loop will get you the path to Visual Studio.C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exefor
0赞 George 1/19/2018 #10

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, + + .CtrlKD

There are a lot of beautifier sites available online.

61赞 patrick 2/22/2018 #11

(Command Window) ( + + ):CtrlAltA

Tools.DiffFiles File1 File2

评论

0赞 niico 6/6/2018
This is the same as another answer
14赞 niico 6/6/2018
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.
2赞 Dejan Dozet 8/12/2020
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?
0赞 Wayne Ivory 6/3/2021
@DejanDozet I believe what niico was referring to is the fact that is mentioned (and pictured) in the first part of the accepted answer by Vladimir.Tools.DiffFiles
20赞 Zaha 7/22/2018 #12

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

评论

2赞 Yury Kozlov 1/27/2019
Unfortunately, this doesn't work when trying to compare two files without solution (for example, when you open VS2017 in folder mode: Open - Folder)
0赞 Ben Palmer 2/18/2021
What I was looking for, didn't think of an extension. Excellent!
55赞 spottedmahn 6/7/2019 #13

Visual Studio extension: File Differ by Mads Kristensen

Compare files screenshot

评论

3赞 Gappy Hilmore 9/18/2021
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.
1赞 Bobbie E. Ray 11/5/2021
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.
0赞 Inshal Irshad 8/18/2022
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.
8赞 Kiryl Khadanovich 3/6/2020 #14

In Visual Studio, the diff can be called using the and then the commandCommand WindowTools.DiffFiles

  1. Open the by hotkeys + , or by menu ViewOther WindowsCommand WindowCommand WindowCtrlWA
  2. Enter command Tools.DiffFiles "FirstFile.cs" "SecondFile.cs"
1赞 Allan Ridolfi 3/2/2022 #15

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

评论

0赞 Ali Karaca 3/10/2022
English version please
2赞 Allan Ridolfi 3/11/2022
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.
1赞 Timothy G. 6/16/2023 #16

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

1赞 Vivek Nuna 8/16/2023 #17

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