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. Directory.GetFiles - how do I get the file names only and not the complete path?

Directory.GetFiles - how do I get the file names only and not the complete path?

Scheduled Pinned Locked Moved C#
questionsysadmindata-structures
7 Posts 4 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.
  • U Offline
    U Offline
    User 3400231
    wrote on last edited by
    #1

    I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated. foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } Many thanks Lorna

    G T P 3 Replies Last reply
    0
    • U User 3400231

      I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated. foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } Many thanks Lorna

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Path Class[^]

      Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

      1 Reply Last reply
      0
      • U User 3400231

        I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated. foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } Many thanks Lorna

        T Offline
        T Offline
        TJS4u
        wrote on last edited by
        #3

        hello DirectoryInfo di = new DirectoryInfo(Path........); FileInfo[] rgFiles = di.GetFiles("*.jpg"); foreach (FileInfo fi in rgFiles) { // add u r needs }

        U 1 Reply Last reply
        0
        • T TJS4u

          hello DirectoryInfo di = new DirectoryInfo(Path........); FileInfo[] rgFiles = di.GetFiles("*.jpg"); foreach (FileInfo fi in rgFiles) { // add u r needs }

          U Offline
          U Offline
          User 3400231
          wrote on last edited by
          #4

          Thanks but is that not the same as what I have and I get the complete path - here is another 2 lines of the code: ArrayList imageNames = new ArrayList(); string imagesDir = Server.MapPath("~/administration/uploaded/"); foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } thanks Lorna

          1 Reply Last reply
          0
          • U User 3400231

            I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated. foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } Many thanks Lorna

            P Offline
            P Offline
            Paul Unsworth
            wrote on last edited by
            #5

            Hi Lorna This should help you...

            DirectoryInfo dir = new DirectoryInfo(@"C:\Test");
            foreach (FileInfo file in dir.GetFiles())
            {
            Console.WriteLine(file.Name);
            }

            oooo, the Jedi's will feel this one....

            U 1 Reply Last reply
            0
            • P Paul Unsworth

              Hi Lorna This should help you...

              DirectoryInfo dir = new DirectoryInfo(@"C:\Test");
              foreach (FileInfo file in dir.GetFiles())
              {
              Console.WriteLine(file.Name);
              }

              oooo, the Jedi's will feel this one....

              U Offline
              U Offline
              User 3400231
              wrote on last edited by
              #6

              Hurray it works! Thank you very much. For any other interested parties, I rewrote as: DirectoryInfo dir = new DirectoryInfo(@Server.MapPath("~/administration/uploaded/")); foreach (FileInfo file in dir.GetFiles()) { imageNames.Add(file.Name); } Which gave me an ArrayList of filenames only (no path).

              P 1 Reply Last reply
              0
              • U User 3400231

                Hurray it works! Thank you very much. For any other interested parties, I rewrote as: DirectoryInfo dir = new DirectoryInfo(@Server.MapPath("~/administration/uploaded/")); foreach (FileInfo file in dir.GetFiles()) { imageNames.Add(file.Name); } Which gave me an ArrayList of filenames only (no path).

                P Offline
                P Offline
                Paul Unsworth
                wrote on last edited by
                #7

                Glad to help. :-D

                oooo, the Jedi's will feel this one....

                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