Sign in to follow this  
BeanDog

Get actual path capitalization in Windows

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :-(

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this