簡體   English   中英

Directory.GetCurrentDirectory()不能在linux上運行?

[英]Directory.GetCurrentDirectory() not working on linux?

所以我正在嘗試創建一個需要在名為“scripts”的子文件夾中讀取腳本的應用程序。 我的代碼我遇到了問題:

string script = Console.ReadLine();
string path = Directory.GetCurrentDirectory();
string sciptpath  = path + "/scripts/" + script;

這在Windows上運行正常。 但是在Linux上(使用Mono Runtime運行)它會轉到當前用戶的主目錄...而不是可執行文件的目錄。 這是一個錯誤嗎? 有人可以建議一個解決方法嗎?

並不是它需要“修復”它是當前目錄不是你想象的那樣。 當前目錄是相對路徑“具有焦點”的目錄。 無論您的EXE在哪里,您當前的目錄都可以在其他任何地方,或者甚至可能在執行期間更改。

你想要的是:

string path = Path.GetDirectoryName(Application.ExecutablePath);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM