简体   繁体   English

退出时设置当前工作目录(.NET Core)

[英]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 CoreC# 中做到这一点

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.CurrentDirectoryDirectory.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.

相关问题 如何使用 .Net Core 在 linux 上获取当前执行目录? - How do I get the current executing directory on linux with .Net Core? 如何将WPF应用程序的current \\ working目录设置为安装路径 - How to set the current\working directory of a WPF application to the installation path Asp.net核心:Selenium在当前目录中看不到geckodriver - Asp.net core: Selenium doesn't see geckodriver in current directory 当进程未在当前目录中启动时,无法加载DLL(.NET Core 2.1.4) - Unable to load DLL when process not started in the current directory (.NET Core 2.1.4) 如何在ASP.NET Core中获取Active Directory当前用户显示名称? - How to get Active Directory current user Display Name in ASP.NET Core? SignalR ASP.NET Core 2上下文。令牌已设置但不起作用 - SignalR ASP.NET Core 2 context.Token set but not working 如何在 ASP.NET Core 中的(有吸引力的报告)Fastreport.NET 中设置指向当前请求路径的超链接 - How to set hyperlink to current request path in (intractive report) Fastreport.NET in ASP.NET Core SharpSVN中工作目录的当前版本 - Current Revision of the working directory in SharpSVN Active Directory 角色 - .net core - Active Directory Roles - .net core .NET Core - 目录权限Linux - .NET Core - directory permissions Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM