Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get correct case of Filename

Get correct case of Filename

Scheduled Pinned Locked Moved C#
question
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DanielWehrle
    wrote on last edited by
    #1

    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

    L C A 3 Replies Last reply
    0
    • D DanielWehrle

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I think there are no differences for File class between upper case or lower case.

      D 1 Reply Last reply
      0
      • D DanielWehrle

        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

        C Offline
        C Offline
        Calla
        wrote on last edited by
        #3

        Windows isn't case sensitive regarding file names. Why do you need to find out the "correct" casing?

        D L D 3 Replies Last reply
        0
        • D DanielWehrle

          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

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #4

          This seems to work:

          string[] files = Directory.GetFiles(@"C:\", "thefile.txt");
          FileInfo f = new FileInfo(files[0]);
          MessageBox.Show(f.FullName);

          [Forum Guidelines]

          1 Reply Last reply
          0
          • C Calla

            Windows isn't case sensitive regarding file names. Why do you need to find out the "correct" casing?

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            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.

            A 1 Reply Last reply
            0
            • C Calla

              Windows isn't case sensitive regarding file names. Why do you need to find out the "correct" casing?

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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.


              A 1 Reply Last reply
              0
              • D David Skelly

                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.

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #7

                "ABCD".ToLower();

                [Forum Guidelines]

                D 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.


                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #8

                  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.

                  [Forum Guidelines]

                  L 1 Reply Last reply
                  0
                  • A AspDotNetDev

                    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.

                    [Forum Guidelines]

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    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.


                    D 1 Reply Last reply
                    0
                    • C Calla

                      Windows isn't case sensitive regarding file names. Why do you need to find out the "correct" casing?

                      D Offline
                      D Offline
                      DanielWehrle
                      wrote on last edited by
                      #10

                      I communicate to a case sensitiv Linux by using a NFS Server. So some incomming calls with wrong case gives errors on Linux side.

                      1 Reply Last reply
                      0
                      • L Lost User

                        I think there are no differences for File class between upper case or lower case.

                        D Offline
                        D Offline
                        DanielWehrle
                        wrote on last edited by
                        #11

                        Not for windows but for Linux. And I comunicate by NFS with a Linux system.

                        1 Reply Last reply
                        0
                        • A AspDotNetDev

                          "ABCD".ToLower();

                          [Forum Guidelines]

                          D Offline
                          D Offline
                          David Skelly
                          wrote on last edited by
                          #12

                          Well, golly gee, Martha, what wonderful things they can do nowadays. Who would have thought of that?

                          D 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            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.


                            D Offline
                            D Offline
                            David Skelly
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • D David Skelly

                              Well, golly gee, Martha, what wonderful things they can do nowadays. Who would have thought of that?

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              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...

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups