1

I'm simply wanting to change the directory of the current path and listing all the files/directories I'm interested in. The default dir for vs 2010 is inside the ./Projects/ directory. But I'm carrying out these 2 commands in sequence w/ the following:

 ...
system ("cd ../../..");
system ("dir"); //This still lists in the command window, the default Projects directory

Any advice?

Bobson
  • 13,498
  • 5
  • 55
  • 80
jerryh91
  • 1,777
  • 10
  • 46
  • 77
  • 1
    there is a system command in c/c++ I am unaware of a system command in c#. What namespace/assembly is it in. – rerun Jun 14 '13 at 14:36

2 Answers2

2

Assuming that the C# tag is accurate, this is not the correct way to do that.

Look at the Directory class. Specifically Directory.EnumerateFiles().

Bobson
  • 13,498
  • 5
  • 55
  • 80
  • @jerryh91 - I don't use C++, so no, and if you want an answer for C++ you should tag the question that way. However, a quick search turned up [this answer](http://stackoverflow.com/a/612176/298754) which might help. – Bobson Jun 14 '13 at 14:55
1

With most implementations of the "system" call each call to system runs in its own environment, inherited from the parent but not carried from previous calls to system. You cannot carry state from one system() call to another, like the current directory, each will start in the directory of the parent process. You need to either have one call to system which runs both the "cd" and "dir" commands, or change the directory of your main process and then list the files afterward.

Directory.SetCurrentDir should take care of changing the directory, after that the system command will behave as expected.

As @Bobson mentions though, if what you want is a list of files you can actually use in your program, the EnumerateFiles method is the way to go.

Tom Scogland
  • 937
  • 5
  • 12