提问人:Libor Zapletal 提问时间:12/7/2012 最后编辑:Peter MortensenLibor Zapletal 更新时间:8/16/2023 访问量:494805
在 Visual Studio 中比较两个文件
Compare two files in Visual Studio
问:
我在 Visual Studio 2012 中看到了新的比较工具,用于比较两个文件或文件的两个版本。我喜欢。但是当我试图找到它时,我无法找到它,因为我不使用 TFS。
有没有办法使用 Visual Studio 中的内置功能(但没有 TFS)比较两个文件?
答:
我一直是 WinMerge 的粉丝,这是一个开源项目。可以很容易地将其插入 Visual Studio。
将向您展示如何执行此操作
评论
可以从 Visual Studio 库试用 VSCommands 扩展。
最新版本允许您选择两个文件并进行比较:
评论
我也有这个问题。没有 TFS,但我发现将文件类型与文件比较工具关联一文很有帮助。
具体来说,步骤 1b。
打开 Visual Studio 命令提示符并导航到 Common7/IDE 文件夹,然后键入
tf diff /configure
还有一个名为 CompareFiles 的 Visual Studio 扩展,它除了将“比较文件”条目添加到“解决方案资源管理器”上下文菜单之外,不执行任何其他操作。它调用内置的 Visual Studio 差异工具。
以防万一有人(像我一样)不想安装像 VSCommands 这样的多合一扩展......
评论
可以从 Visual Studio 开发人员命令提示符调用,或者,如果 Visual Studio 实例已在运行,则可以在命令窗口中键入,并附带方便的文件名完成:devenv.exe /diff list1.txt list2.txt
Tools.DiffFiles
评论
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
评论
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 Essential → Diff selected files:
It's also available as a separate extension.
评论
VS 2015
Diff selected files
An excellent lightweight tool that gets the job done is the File Comparer Visual Studio extension by Akhil Mittal.
评论
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 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
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 footnotevspath
devenv.exe
*) )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:
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 (the path to ) differs depending on your version of Visual Studio, I am describing how you can find it out (Windows 10):vsPath
DEVENV.exe
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
评论
C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe
for
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
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
Step 3: Merge the files
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.
(Command Window) ( + + ):CtrlAltA
Tools.DiffFiles File1 File2
评论
Tools.DiffFiles
For Visual Studio 2019, the File Differ plugin allows to compare files in distinct projects:
You can install it from here:
OLD:
The problem is that you can't compare files in different projects, but you can copy the files in the same project to compare...
评论
Visual Studio extension: File Differ by Mads Kristensen
评论
In Visual Studio, the diff can be called using the and then the commandCommand Window
Tools.DiffFiles
- Open the by hotkeys + , or by menu View → Other Windows → Command Window
Command Window
CtrlWA - Enter command
Tools.DiffFiles "FirstFile.cs" "SecondFile.cs"
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
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
评论
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}":
This will open up the a diff comparison tab like so:
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.
Reference: https://devblogs.microsoft.com/visualstudio/visual-studio-2022-17-7-now-available/#comparing-files
评论