简体   繁体   中英

Can I change curent directory using c# console app?

I would like to create small applications to manage my projects, should work so that after you select an item from the list of projects then the console changes the current directory to the project and exits program leaving you console in corect dir, then you can run, for example, vs code or http-server.

I tryed
Environment.CurrentDirectory = direcory
Directory.SetCurrentDirectory(directory)
neither of it worked for me :/

I think this should do the trick. Also, you can execute some command on it from the code:

var startInfo = new ProcessStartInfo
{
    WorkingDirectory = @"SomeDirectory",
    FileName = "cmd.exe",
    // Arguments = "start http-server"
};
var process = new Process {StartInfo = startInfo};
process.Start();

If I understand your question correctly, essentially you want to write a C# program that performs a similar function to the CD command in the Command Prompt environment.

Unfortunately you cannot do this from C# program as the program is started in its own isolated AppDomain, which is thrown away (along with your new working directory) when your program completes. On top of this you cannot influence the creation of the AppDomain by the launching process (the command prompt).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM