[英]Set current working directory on exit (.NET Core)
I would like to have my application set the current working directory after my application is ran, meaning that, for example:我想让我的应用程序在我的应用程序运行后设置当前工作目录,这意味着,例如:
$ pwd
/my/directory
$ dotnet MyApp.dll
$ pwd
/another/place/somewhere
How can I do that in C# using .NET Core in a cross-platform way ?如何以跨平台的方式使用 .NET Core在C# 中做到这一点?
I looked at Environment.CurrentDirectory
and Directory.SetCurrentDirectory
but for both of them the working directory is reset to its original state when the application exits, as indicated in the documentation for Directory.SetCurrentDirectory
.我查看了
Environment.CurrentDirectory
和Directory.SetCurrentDirectory
但对于它们,当应用程序退出时,工作目录将重置为其原始状态,如Directory.SetCurrentDirectory
的文档中所示。
When the application terminates, the working directory is restored to its original location (the directory where the process was started).
当应用程序终止时,工作目录将恢复到其原始位置(进程启动的目录)。
https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.setcurrentdirectory?view=netframework-4.7.2#remarks https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.setcurrentdirectory?view=netframework-4.7.2#remarks
You can't.你不能。
The current working directory is a property of each process.当前工作目录是每个进程的一个属性。
To change it in the terminal, you need to find a way to tell the terminal to do it.要在终端中更改它,您需要找到一种方法来告诉终端执行此操作。 A program that the terminal launches can change its own current directory, but not that of the calling terminal.
终端启动的程序可以更改自己的当前目录,但不能更改调用终端的当前目录。
This is why cd
is a built-in in shells.这就是为什么
cd
是 shell 内置的。 If it was an external program, it couldn't change the working directory of the shell/terminal.如果是外部程序,则无法更改 shell/终端的工作目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.