Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I obtain the case-sensitive path on Windows?

Tags:

c#

.net

filepath

I need to know which is the real path of a given path.

For example:

The real path is: d:\src\File.txt
And the user give me: D:\src\file.txt
I need as a result: d:\src\File.txt

like image 853
Rodrigo Avatar asked Jan 21 '11 19:01

Rodrigo


People also ask

Are Windows path case-sensitive?

Windows file system treats file and directory names as case-insensitive.

How do I turn on case sensitivity in Windows?

Type the following command to enable NTFS to treat the folder's content as case sensitive and press Enter: fsutil.exe file SetCaseSensitiveInfo C:\folder\path enable In the command, remember to include the path to the folder you want to enable case sensitivity.

When working with a Windows 10 PC which of the following are case-sensitive?

Windows 10 now offers an optional case-sensitive file system, just like Linux and other UNIX-like operating systems. All Windows processes will handle case-sensitive files and folders properly if you enable this feature. In other words, they'll see “file” and “File” as two separate files.

How do I get a path in Windows?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).


1 Answers

You can use this function:

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)] static extern uint GetLongPathName(string ShortPath, StringBuilder sb, int buffer);  [DllImport("kernel32.dll")] static extern uint GetShortPathName(string longpath, StringBuilder sb, int buffer);   protected static string GetWindowsPhysicalPath(string path) {         StringBuilder builder = new StringBuilder(255);          // names with long extension can cause the short name to be actually larger than         // the long name.         GetShortPathName(path, builder, builder.Capacity);          path = builder.ToString();          uint result = GetLongPathName(path, builder, builder.Capacity);          if (result > 0 && result < builder.Capacity)         {             //Success retrieved long file name             builder[0] = char.ToLower(builder[0]);             return builder.ToString(0, (int)result);         }          if (result > 0)         {             //Need more capacity in the buffer             //specified in the result variable             builder = new StringBuilder((int)result);             result = GetLongPathName(path, builder, builder.Capacity);             builder[0] = char.ToLower(builder[0]);             return builder.ToString(0, (int)result);         }          return null; } 
like image 110
Borja Avatar answered Oct 13 '22 23:10

Borja