Possible Duplicate:
Best way to determine if two path reference to same file in C#
So I have two Windows filenames I need to compare to determine if they are the same. One the user gave me, one given to me by another program. So how should you compare:
C:\Program Files\Application1\APP.EXE C:\Progra~1\Applic~1\APP.EXE C:\program files\applic~1\app.exe
I can't seem to find a way to consistently 'normalize' the path, I tried using Path.GetFullPath(path) and new FileInfo(path).FullName and neither seem to resolve this.
UPDATE:
Path.GetFullPath(path) will correct the short to long name conversion but it will not normalize case. Thus a StringComparer.OrdinalIgnoreCase.Equals(path1, path2) is required.
Comparison Using diff GNU diff can compare two files line by line and report all the differences. The output indicates that file1 and file3 are the same, and file2 has different contents.
Use the diff command to compare text files. It can compare single files or the contents of directories. When the diff command is run on regular files, and when it compares text files in different directories, the diff command tells which lines must be changed in the files so that they match.
Start Windiff.exe. On the File menu, click Compare Files. In the Select First File dialog box, locate and then click a file name for the first file in the comparison, and then click Open. In the Select Second File dialog box, locate and then click a file name for the second file in the comparison, and then click Open.
You will need Path.GetFullPath()
+ case insensitive string comparison.
Running the following code:
using System;
using System.IO;
class Test {
static void Main ()
{
//string [] str = new string[] {@"c:\program files\vim\vim72", @"c:\progra~1\vim\vim72"};
string [] str = new string[] {@"c:\program files\Refere~1\microsoft", @"c:\progra~1\Refere~1\microsoft"};
foreach (string s in str) {
// Call s = Environment.ExpandEnvironmentVariables (s) if needed.
Console.WriteLine (Path.GetFullPath (s));
}
}
}
gives:
c:\program files\Reference Assemblies\microsoft
c:\Program Files\Reference Assemblies\microsoft
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With