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. .NET (Core and Framework)
  4. Get the proper path name, proper case I mean.

Get the proper path name, proper case I mean.

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasecomperformancehelptutorial
6 Posts 2 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.
  • A Offline
    A Offline
    AndrewVos
    wrote on last edited by
    #1

    I'm looking for a way to get the proper path name, for example if I have: "c:\\program files" I would expect "C:\\Program Files". Any ideas? I'm using the code below now, but the use of GetFileSystemInfos is going to be a performance problem some time... private string fixPathName(string path) { string[] pathParts = path.Split(Path.DirectorySeparatorChar); string fixedPath = null; for (int index = 0; index < pathParts.Length; index++) { string pathPart = pathParts[index]; if (index == 0) { fixedPath = pathPart; fixedPath = Path.GetFullPath(fixedPath); } else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath)); string pathProperName = paths[0].FullName; fixedPath = pathProperName; } } return fixedPath; }


    www.wickedorange.com www.andrewvos.com

    D 2 Replies Last reply
    0
    • A AndrewVos

      I'm looking for a way to get the proper path name, for example if I have: "c:\\program files" I would expect "C:\\Program Files". Any ideas? I'm using the code below now, but the use of GetFileSystemInfos is going to be a performance problem some time... private string fixPathName(string path) { string[] pathParts = path.Split(Path.DirectorySeparatorChar); string fixedPath = null; for (int index = 0; index < pathParts.Length; index++) { string pathPart = pathParts[index]; if (index == 0) { fixedPath = pathPart; fixedPath = Path.GetFullPath(fixedPath); } else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath)); string pathProperName = paths[0].FullName; fixedPath = pathProperName; } } return fixedPath; }


      www.wickedorange.com www.andrewvos.com

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

      There is no "proper name", since path specifications are not case sensitive. So, it begs the question, why would you care about the case of the characters??

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        There is no "proper name", since path specifications are not case sensitive. So, it begs the question, why would you care about the case of the characters??

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        A Offline
        A Offline
        AndrewVos
        wrote on last edited by
        #3

        Well, so it looks right to the user. I'm working on a bit of autocomplete code (like the Run dialog). I do know about ComboBox/TextBox autocomplete, but it doesn't meet the specs.


        www.wickedorange.com www.andrewvos.com

        1 Reply Last reply
        0
        • A AndrewVos

          I'm looking for a way to get the proper path name, for example if I have: "c:\\program files" I would expect "C:\\Program Files". Any ideas? I'm using the code below now, but the use of GetFileSystemInfos is going to be a performance problem some time... private string fixPathName(string path) { string[] pathParts = path.Split(Path.DirectorySeparatorChar); string fixedPath = null; for (int index = 0; index < pathParts.Length; index++) { string pathPart = pathParts[index]; if (index == 0) { fixedPath = pathPart; fixedPath = Path.GetFullPath(fixedPath); } else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath)); string pathProperName = paths[0].FullName; fixedPath = pathProperName; } } return fixedPath; }


          www.wickedorange.com www.andrewvos.com

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

          AndrewVos wrote:

          else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath));

          If I read this correctly, you can replace all this sutff with this:

          FileInfo fileObject = new FileInfo(fixedPath);
          fixedPath = fileObject.FullName;
          

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          A 2 Replies Last reply
          0
          • D Dave Kreskowiak

            AndrewVos wrote:

            else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath));

            If I read this correctly, you can replace all this sutff with this:

            FileInfo fileObject = new FileInfo(fixedPath);
            fixedPath = fileObject.FullName;
            

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            A Offline
            A Offline
            AndrewVos
            wrote on last edited by
            #5

            string fixedPath = @"c:\program files\"; FileInfo fileObject = new FileInfo(fixedPath); fixedPath = fileObject.FullName; MessageBox.Show(fixedPath); This would return "c:\program files".


            www.wickedorange.com www.andrewvos.com

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              AndrewVos wrote:

              else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath));

              If I read this correctly, you can replace all this sutff with this:

              FileInfo fileObject = new FileInfo(fixedPath);
              fixedPath = fileObject.FullName;
              

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              A Offline
              A Offline
              AndrewVos
              wrote on last edited by
              #6

              Ok, I've got everything working. It seems GetDirectories is actually fast enough when using a search pattern. Thanks for reading through the code, and here's what I'm using now. public List Generate(string generationString) { List results = new List(); string[] pathParts = generationString.Split(Path.DirectorySeparatorChar); DirectoryInfo currentDirectory = null; for (int index = 0; index < pathParts.Length;index++ ) { string pathPart = pathParts[index]; if (index == 0) { //The first part will be the drive. DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in drives) { if (drive.Name.StartsWith(pathPart, StringComparison.InvariantCultureIgnoreCase)) { currentDirectory = drive.RootDirectory; } } if (currentDirectory == null) { break; } } else if ((index == pathParts.Length - 1) ) { //The last part could be anything, so we do a wildcard search. try { results.AddRange(Directory.GetDirectories(currentDirectory.FullName, pathPart + "*")); results.AddRange(Directory.GetFiles(currentDirectory.FullName, pathPart + "*")); } catch { } } else { //Just add the first result to the path. DirectoryInfo[] searchResults = currentDirectory.GetDirectories(pathPart); if (searchResults.Length == 0) { break; } else { currentDirectory = searchResults[0]; } } } return results; }


              www.wickedorange.com www.andrewvos.com

              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