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. DirectoryInfo does not return full path

DirectoryInfo does not return full path

Scheduled Pinned Locked Moved C#
helpquestion
3 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.
  • M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #1

    Hi All. I have a slight problem in that my DirectoryInfo does not return the full path. Does anybody know if there is a limitation to the DirectoryInfo? The path to the files is something like this: C:\\Folder1\\Folder2\\Folder3\\Folder4\\Folder5 My code is as follows:

    private void getAllFiles(string sExt, string sFolder)
    {
    DirectoryInfo di = new DirectoryInfo(sFolder);
    FileInfo[] fi = di.GetFiles(sExt, SearchOption.AllDirectories);
    //List<FileInfo> fi = new List<FileInfo>(di.EnumerateFiles(sExt, SearchOption.AllDirectories));
    foreach (FileInfo file in fi)
    {
    AddNewFile(Path.Combine(di.ToString(), file.ToString()));
    counter++;
    }
    lblTotalFiles.Text = counter.ToString();
    //SetText(counter.ToString());
    }

    FileInfo seems to get the location of the file as it shows me the file it picked up but when the path of DirectoryIfo is passed on it excludes Folder5 thus the object I am passing it to doesn't find the file in question. Is there anyway I can fix this?

    Excellence is doing ordinary things extraordinarily well.

    A 1 Reply Last reply
    0
    • M MumbleB

      Hi All. I have a slight problem in that my DirectoryInfo does not return the full path. Does anybody know if there is a limitation to the DirectoryInfo? The path to the files is something like this: C:\\Folder1\\Folder2\\Folder3\\Folder4\\Folder5 My code is as follows:

      private void getAllFiles(string sExt, string sFolder)
      {
      DirectoryInfo di = new DirectoryInfo(sFolder);
      FileInfo[] fi = di.GetFiles(sExt, SearchOption.AllDirectories);
      //List<FileInfo> fi = new List<FileInfo>(di.EnumerateFiles(sExt, SearchOption.AllDirectories));
      foreach (FileInfo file in fi)
      {
      AddNewFile(Path.Combine(di.ToString(), file.ToString()));
      counter++;
      }
      lblTotalFiles.Text = counter.ToString();
      //SetText(counter.ToString());
      }

      FileInfo seems to get the location of the file as it shows me the file it picked up but when the path of DirectoryIfo is passed on it excludes Folder5 thus the object I am passing it to doesn't find the file in question. Is there anyway I can fix this?

      Excellence is doing ordinary things extraordinarily well.

      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      Kwagga wrote:

      FileInfo[] fi = di.GetFiles(sExt, SearchOption.AllDirectories); .... Path.Combine(di.ToString(), file.ToString())

      These lines may not be doing what you assume. SearchOption.AllDirectories tells GetFiles to search the starting directory and all subdirectories. The ToString() method of FileInfo's created by GetFiles returns just the filename without any path information. Your use of Path.Combine creates the correct full path only for files in the starting directory. If your aim is to pass the full path of each file to the AddNewFile method just use the FileInfo.FullName property without Path.Combine. i.e. AddNewFile(file.FullName); Alan.

      M 1 Reply Last reply
      0
      • A Alan N

        Kwagga wrote:

        FileInfo[] fi = di.GetFiles(sExt, SearchOption.AllDirectories); .... Path.Combine(di.ToString(), file.ToString())

        These lines may not be doing what you assume. SearchOption.AllDirectories tells GetFiles to search the starting directory and all subdirectories. The ToString() method of FileInfo's created by GetFiles returns just the filename without any path information. Your use of Path.Combine creates the correct full path only for files in the starting directory. If your aim is to pass the full path of each file to the AddNewFile method just use the FileInfo.FullName property without Path.Combine. i.e. AddNewFile(file.FullName); Alan.

        M Offline
        M Offline
        MumbleB
        wrote on last edited by
        #3

        Thanks Alan. That resolved my issue. Greatly appreciated.

        Excellence is doing ordinary things extraordinarily well.

        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