我在一个C# WPF应用程序中有两个字符串,想要生成一个统一的差异文件(格式为GNU diff符号,类似于Subversion和TortoiseSVN中使用的补丁)。
是否有一个库可以帮助我完成这个任务,而不是重新发明轮子?
我已经在谷歌上搜索了很久,但没有成功。 :(
是的,我知道这是一个古老的问题,但它仍然与我的当前项目相关。因此,为了帮助他人,我提供了一个博客文章链接,展示了我编写的用于调用GNU diff.exe程序的程序。
https://renniestechblog.com/information/37-generate-diff-and-fuzzy-patch-programs
以下是最重要的部分,其中调用了GNU diff.exe程序。(此代码引用了程序其他部分的一些内容,但应该可以猜出它的工作原理。)
/// <summary>
/// Method to use the GNU diff.exe program to create a .diff file for a Roslyn file which has
/// been modified.
///
/// This currently uses the GNU diff.exe program that happens to have been installed on my
/// developer PC as part of the GitHub Desktop for Windows program installation.
/// </summary>
private static void CreateDiffFile(string archiveFileName, string currentFileName)
{
const string CGnuDiffExe =
@"C:\Users\rp\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\usr\bin\diff.exe";
string diffFileName = GetDiffFileName(currentFileName) + ".diff";
Directory.CreateDirectory(Path.GetDirectoryName(diffFileName));
using (Process windowsProcess = new Process())
{
windowsProcess.StartInfo.UseShellExecute = false;
windowsProcess.StartInfo.FileName = CGnuDiffExe;
windowsProcess.StartInfo.Arguments =
"-u -r \"" + archiveFileName + "\" \"" + currentFileName + "\"";
windowsProcess.StartInfo.RedirectStandardOutput = true;
if (!windowsProcess.Start())
DisplayErrorOrInfo("Unexpected result for Process.Start()");
File.WriteAllText(diffFileName, windowsProcess.StandardOutput.ReadToEnd());
windowsProcess.WaitForExit();
}
Console.WriteLine("Diff file created: " + diffFileName);
}
http://razor.occams.info/code/diff/
请注意,它是根据PERL许可证发布的(请参见网站)。