• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:

Need to get directory info from parent directories

Hi

I need to get the files at a parent (or maybe its the grand parent)  directory

I am doing this in my console app:

            DirectoryInfo di = new DirectoryInfo(@"cd  ..\..\dll");
            FileInfo[] dllFiles = di.GetFiles("*.dll");


            foreach (FileInfo fi in dllFiles)
            {
                Console.WriteLine(fi);
            }

I am sure it exits if I do a cd ..\..\dll from a command line in the folder with the executable I get there

what am i doing wrong?

Thanks in advanced
chuck
0
Charles Baldo
Asked:
Charles Baldo
  • 2
1 Solution
 
jgordosCommented:
You're passing in a shell command to DirectoryInfo's constructor.

It accepts a PATH, not a command.

take the cd out.

-john
0
 
anarki_jimbelCommented:
No, this does not work this way.

Try my example:
        static void Main(string[] args)
        {
            string currentDir = Directory.GetCurrentDirectory();
            Console.WriteLine(currentDir);
            DirectoryInfo diParent = new DirectoryInfo(currentDir.Substring(0, currentDir.LastIndexOf(@"\")));
            DirectoryInfo diGrandParent = diParent.Parent;
 
            
            Console.WriteLine(diParent.FullName);
            FileInfo[] infos = diParent.GetFiles();
 
            Console.WriteLine("Files in Parent Dir: =============");
            for (int i = 0; i < infos.Length - 1; i++)
            {
                Console.WriteLine(infos[i].Name);
            }
            Console.WriteLine("====================");
 
            Console.WriteLine("Files in GrandParent Dir: =============");
            infos = diGrandParent.GetFiles();
            for (int i = 0; i < infos.Length - 1; i++)
            {
                Console.WriteLine(infos[i].Name);
            }
            Console.WriteLine("====================");
 
            Console.Read();
        }

Open in new window

0
 
jgordosCommented:
use ".." for parent
use "..\.." for grandparent

make sure you put the @, or else you need to escape the "\" because it has special meaning in strings...

so,

@"..\..\.." is okay
and
"..\\..\\.." is okay

This:
"..\.." is not.

-john
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now