开发者

C# - Unified diff/patch creator [closed]

开发者 https://www.devze.com 2023-02-03 23:29 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answer开发者_JS百科s.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answer开发者_JS百科s.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 4 years ago.

Improve this question

I have two strings in a C# WPF application and would like to generate a unified diff file (in the format of GNU diff notation, like patches used in Subversion and TortoiseSVN).

Is there a library that can do this for me instead of recreating the wheel?

I have been searching google without success. :(


Two alternatives:

http://diffplex.codeplex.com

http://htmldiff.codeplex.com


Yes, I know that this is an old, old question, but it was still relevant for my current project. So just in case this is helpful to others I'll provide a link to a blog article showing the program I ended up writing to invoke the GNU diff.exe program.

https://renniestechblog.com/information/37-generate-diff-and-fuzzy-patch-programs

Here's the most important part, where the GNU diff.exe program is invoked. (This code references some things in other parts of the program, but it should be possible to guess how it works.)

  /// <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);
  }


Found one solution. Note the assembly is not supported and reportedly buggy, but I used it to compare complex XHTML and it worked great!

http://razor.occams.info/code/diff/

Note it is released under PERL licensing (see site).

Download Diff.dll and add it to your solution.

There is a neat example on the site.

0

精彩评论

暂无评论...
验证码 换一张
取 消