Get correct case of Filename
-
Hi, I make somethin like: File.WriteAllBytes("C:\\test.TXT", mybytes); FileInfo fi = new FileInfo("C:\\test.txt"); Now fi.Fullname returns the lower case FileName, but I need to get the correct cased file name. Is there any way whithout doing a enumerate directory, and compare the strings? Thanks -- Daniel
-
Hi, I make somethin like: File.WriteAllBytes("C:\\test.TXT", mybytes); FileInfo fi = new FileInfo("C:\\test.txt"); Now fi.Fullname returns the lower case FileName, but I need to get the correct cased file name. Is there any way whithout doing a enumerate directory, and compare the strings? Thanks -- Daniel
-
Hi, I make somethin like: File.WriteAllBytes("C:\\test.TXT", mybytes); FileInfo fi = new FileInfo("C:\\test.txt"); Now fi.Fullname returns the lower case FileName, but I need to get the correct cased file name. Is there any way whithout doing a enumerate directory, and compare the strings? Thanks -- Daniel
-
Hi, I make somethin like: File.WriteAllBytes("C:\\test.TXT", mybytes); FileInfo fi = new FileInfo("C:\\test.txt"); Now fi.Fullname returns the lower case FileName, but I need to get the correct cased file name. Is there any way whithout doing a enumerate directory, and compare the strings? Thanks -- Daniel
This seems to work:
string[] files = Directory.GetFiles(@"C:\", "thefile.txt");
FileInfo f = new FileInfo(files[0]);
MessageBox.Show(f.FullName); -
Windows isn't case sensitive regarding file names. Why do you need to find out the "correct" casing?
Calla wrote:
Windows isn't case sensitive regarding file names.
Windows isn't but c# string comparison is. "abcd" is not the same as "ABCD", so if you are going to compare file names in your program, you have to get the correct case or it won't work.
-
Windows isn't case sensitive regarding file names. Why do you need to find out the "correct" casing?
other operating systems may have case-sensitive file systems, Unix does. When you want to use your app on such system, or you want to store data on a non-Windows external storage system, the app needs to take care of casing. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
Calla wrote:
Windows isn't case sensitive regarding file names.
Windows isn't but c# string comparison is. "abcd" is not the same as "ABCD", so if you are going to compare file names in your program, you have to get the correct case or it won't work.
"ABCD".ToLower();
-
other operating systems may have case-sensitive file systems, Unix does. When you want to use your app on such system, or you want to store data on a non-Windows external storage system, the app needs to take care of casing. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
One does have to wonder, though, how the app would get an incorrectly-cased file name. I'd expect the user to enter in the filename with the correct case on an OS that could have two filenames that are the same aside from case. If it was a file created by the app, I'd expect the app to already know the filename. Also, C# is rarely used on Unix systems (though, due to Mono, it can be used there). I wonder if the OP actually needs this functionality, or if the question was asked out of curiosity. Only in very strange instances do I see this as useful.
-
One does have to wonder, though, how the app would get an incorrectly-cased file name. I'd expect the user to enter in the filename with the correct case on an OS that could have two filenames that are the same aside from case. If it was a file created by the app, I'd expect the app to already know the filename. Also, C# is rarely used on Unix systems (though, due to Mono, it can be used there). I wonder if the OP actually needs this functionality, or if the question was asked out of curiosity. Only in very strange instances do I see this as useful.
aspdotnetdev wrote:
I'd expect the user to enter in the filename with the correct case
Most of the time the problem is the app runs on Windows and doesn't mind casing errors until you issue a file command to a Unix-based file server. Typically it isn't a file name clash, it is a no-such-file problem. one way of getting in trouble is by having hard-coded extensions, say in OpenFileDialog filters, or in a Path.ChangeExtension() call. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
Windows isn't case sensitive regarding file names. Why do you need to find out the "correct" casing?
I communicate to a case sensitiv Linux by using a NFS Server. So some incomming calls with wrong case gives errors on Linux side.
-
Not for windows but for Linux. And I comunicate by NFS with a Linux system.
-
"ABCD".ToLower();
Well, golly gee, Martha, what wonderful things they can do nowadays. Who would have thought of that?
-
aspdotnetdev wrote:
I'd expect the user to enter in the filename with the correct case
Most of the time the problem is the app runs on Windows and doesn't mind casing errors until you issue a file command to a Unix-based file server. Typically it isn't a file name clash, it is a no-such-file problem. one way of getting in trouble is by having hard-coded extensions, say in OpenFileDialog filters, or in a Path.ChangeExtension() call. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
It's even more complicated than that. Unix sees myfile.txt and MyFile.txt as two different files and lets you create these two files in the same directory, side by side. Windows doesn't like that and won't let you do it. Most C# programs probably never have to interoperate between Windows and Unix. It's much more of a problem with something like Java.
-
Well, golly gee, Martha, what wonderful things they can do nowadays. Who would have thought of that?
Check out the overloads of String.Compare. You've got all kinds of options, including ignoring case, when comparing strings.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...