English 中文(简体)
What is the right way to compare two filenames to see if they are the same file? [duplicate]
原标题:
This question already has answers here:
Closed 10 years ago.

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 FilesApplication1APP.EXE
C:Progra~1Applic~1APP.EXE
C:program filesapplic~1app.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.

最佳回答

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 filesvimvim72", @"c:progra~1vimvim72"};
  string [] str = new string[] {@"c:program filesRefere~1microsoft", @"c:progra~1Refere~1microsoft"};
  foreach (string s in str) {
   // Call s = Environment.ExpandEnvironmentVariables (s) if needed.
   Console.WriteLine (Path.GetFullPath (s));
  }
 }
}

gives:

c:program filesReference Assembliesmicrosoft
c:Program FilesReference Assembliesmicrosoft
问题回答

a short test run says that the below code will work for the paths given:

bool CompareFileName(string file1, string file2)
        {
            var directory1 = Path.GetDirectoryName(file1);
            var directory2 = Path.GetDirectoryName(file2);
            var fileName1 = Path.GetFileName(file1);
            var fileName2 = Path.GetFileName(file2);

            return directory1.Equals(directory2, StringComparison.InvariantCultureIgnoreCase) &&
                   fileName1.Equals(fileName2, StringComparison.InvariantCultureIgnoreCase);
        }

this assumes windows platform (an assumption made due to the windows centric paths given as example paths)

I use the FileInfo object. If you create a fileinfo object of a file that actually exists the Directory property gives a nicely formatted path name.

You also get the additional benefit of being able to test if the file actually exists.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签