16

I am creating a simple explorer program for an assignment on c# and have the directory set to c:\\Windows

How you would be able to change the directory from the default windows to something else in the console.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Anthony Bond
  • 161
  • 1
  • 1
  • 3

3 Answers3

18
Directory.SetCurrentDirectory(@"c:\program files\");
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • say if i had multiple methods how would i make it so that if i type 4 on an options screen and change to a different directory how would i get it to keep the new directory when it goes back to start screen ? – Anthony Bond Jan 13 '13 at 16:12
  • Please be cautious! This API call changes the current working directory of your application. So, if you load any assemblies at run time which were present at the original location (from where your executable had started) will fail to load. – RBT Jan 20 '17 at 03:01
7

You can set Environment.CurrentDirectory property for your directory path.

Gets or sets the fully qualified path of the current working directory.

public static void Main(string[] args)
{
    Environment.CurrentDirectory = "C:\\Windows";
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
5

Try setting Environment.CurrentDirectory to the value you want.

In your case:

Environment.CurrentDirectory = "C:\\Windows";
Candide
  • 30,469
  • 8
  • 53
  • 60