Get actual path capitalization in Windows

Started by
5 comments, last by BeanDog 16 years ago
Windows filenames are case-insensitive. So a user tells me to open file "TEST.TXT", even though it's actually stored as "Test.txt", and it opens correctly. However, I need to know the officially-correct capitalization of the file--the filename as actually stored in the file system--for some processing I do later. If I have a path like "C:\TEST.TXT", how do I query for the actual capitalization of that filename? Any way is OK, but if there's an easy way in C# that'd be best. Thanks!
Advertisement
To the best of my knowledge there isnt a method of taking TEST.TXT and converting it into Test.txt or whatever. This is because capitalization is irrelevant on Windows and so you should never need to do this. The best thing you could do, probably, is to search for that file in the directory and use whatever file name the search returns.
Actually, the reason is because it's on a Samba share--so it's actually stored on Linux, but I'm accessing it from Windows. So the capitalization enforcement is a bit mixed. If I write to the file with a different capitalization, it will create a second file :-(
In this case I would enforce the case-sensitivity. If the user asks you to open TEST.TXT but only Test.txt exists, you should return a file not found error. Otherwise, how would your program handle the situation in which both TEST.TXT and Test.txt existed?
The easiest option I can think of is to use FindFirstFile().

That along with FindNextFile() should get you the proper filename, and tell you if there's any other files that also match that name.

What you do with that information depends on what makes sense for your app.

It also looks like your problem may vary with the samba settings. Look here for more info on how samba handles filenames. Changing some of those settings on your share may help.
Doesn't System.IO.FileInfo.Name return the name of the file with its capitalization?
Quote:Original post by SiCrane
Doesn't System.IO.FileInfo.Name return the name of the file with its capitalization?


Incredibly, it does not. If I get "new FileInfo("C:\\TEST.TXT").Name", it's going to use the capitalization I passed into the constructor.

This topic is closed to new replies.

Advertisement