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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C# / LINQ Directory.GetFiles

C# / LINQ Directory.GetFiles

Scheduled Pinned Locked Moved C#
csharpdatabaselinqquestion
5 Posts 4 Posters 1 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.
  • W Offline
    W Offline
    Wheels012
    wrote on last edited by
    #1

    Good morning. I just started using Directory.GetFiles and I am wondering why it returns the path including the file name. It is my expectation that it would only return a list of file names as I already know the path. I have the follwoing code:

    internal static void DemoGrouping()
    {
    string[] files = Directory.GetFiles(@"C:\Documents and Settings\TEMP");

            //Convention: Name method so that it explains what the query is doing
            var fileList = from file in files
                           where file.Length > 1
                           orderby file.Length                       
                           group file by file.Length;
    
    
            foreach (var group in fileList)
            {
                //First print the group
               Console.WriteLine(group.Key);
                foreach (string subgroup in group)
                Console.WriteLine("   " + subgroup);
            }
    
    
        }
    

    Without getting into substrings to extract the file name, is there a usage of Directory.GetFiles that accomplishes what I am looking for? Thank you, WHEELS

    R realJSOPR L 3 Replies Last reply
    0
    • W Wheels012

      Good morning. I just started using Directory.GetFiles and I am wondering why it returns the path including the file name. It is my expectation that it would only return a list of file names as I already know the path. I have the follwoing code:

      internal static void DemoGrouping()
      {
      string[] files = Directory.GetFiles(@"C:\Documents and Settings\TEMP");

              //Convention: Name method so that it explains what the query is doing
              var fileList = from file in files
                             where file.Length > 1
                             orderby file.Length                       
                             group file by file.Length;
      
      
              foreach (var group in fileList)
              {
                  //First print the group
                 Console.WriteLine(group.Key);
                  foreach (string subgroup in group)
                  Console.WriteLine("   " + subgroup);
              }
      
      
          }
      

      Without getting into substrings to extract the file name, is there a usage of Directory.GetFiles that accomplishes what I am looking for? Thank you, WHEELS

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Don't think there is. Alernatively though, use DirectoryInfo.GetFiles(). This gives you an array of FileInfo, and from that you can just get the file name (.Name property).

      Regards, Rob Philpott.

      1 Reply Last reply
      0
      • W Wheels012

        Good morning. I just started using Directory.GetFiles and I am wondering why it returns the path including the file name. It is my expectation that it would only return a list of file names as I already know the path. I have the follwoing code:

        internal static void DemoGrouping()
        {
        string[] files = Directory.GetFiles(@"C:\Documents and Settings\TEMP");

                //Convention: Name method so that it explains what the query is doing
                var fileList = from file in files
                               where file.Length > 1
                               orderby file.Length                       
                               group file by file.Length;
        
        
                foreach (var group in fileList)
                {
                    //First print the group
                   Console.WriteLine(group.Key);
                    foreach (string subgroup in group)
                    Console.WriteLine("   " + subgroup);
                }
        
        
            }
        

        Without getting into substrings to extract the file name, is there a usage of Directory.GetFiles that accomplishes what I am looking for? Thank you, WHEELS

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        This (or something close to it) should get you just the file name.

        string fileName = System.IO.Path.GetFileName(path);

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        1 Reply Last reply
        0
        • W Wheels012

          Good morning. I just started using Directory.GetFiles and I am wondering why it returns the path including the file name. It is my expectation that it would only return a list of file names as I already know the path. I have the follwoing code:

          internal static void DemoGrouping()
          {
          string[] files = Directory.GetFiles(@"C:\Documents and Settings\TEMP");

                  //Convention: Name method so that it explains what the query is doing
                  var fileList = from file in files
                                 where file.Length > 1
                                 orderby file.Length                       
                                 group file by file.Length;
          
          
                  foreach (var group in fileList)
                  {
                      //First print the group
                     Console.WriteLine(group.Key);
                      foreach (string subgroup in group)
                      Console.WriteLine("   " + subgroup);
                  }
          
          
              }
          

          Without getting into substrings to extract the file name, is there a usage of Directory.GetFiles that accomplishes what I am looking for? Thank you, WHEELS

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

          GetFiles() and the like return the full path, which is useful as you can ask those methods to work hierarchically (SearhOption.AllDirectories), so just a filename would not always be sufficient. You can extract the filename from a full path using the Path class, you can't do the reverse. BTW: Using DirectoryInfo to get all of them at once is a costly operation if you only are interested in a few of the results returned. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          W 1 Reply Last reply
          0
          • L Luc Pattyn

            GetFiles() and the like return the full path, which is useful as you can ask those methods to work hierarchically (SearhOption.AllDirectories), so just a filename would not always be sufficient. You can extract the filename from a full path using the Path class, you can't do the reverse. BTW: Using DirectoryInfo to get all of them at once is a costly operation if you only are interested in a few of the results returned. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            W Offline
            W Offline
            Wheels012
            wrote on last edited by
            #5

            Thanks to everyone for the excellent replies. WHEELS

            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