簡體   English   中英

如何檢索我正在使用 C# 的存儲庫目錄名稱?

[英]How to retrieve the repository directory name I am in using C#?

所以我目前正在使用特定存儲庫中的 Unity 項目。 我怎樣才能得到我正在使用的這個存儲庫的名稱? 即.git文件夾正上方的文件夾名稱。

我這樣做的方式是這樣的:

Directory.GetCurrentDirectory().GetParentDirectory().GetParentDirectory().GetParentDirectory().GetFilename();

但是,這感覺不是最好的方法,因為它幾乎就像我在硬編碼一樣?

還有什么其他方法可以做到這一點?

您可能會從一個已知的文件夾開始,例如Application.dataPath ,它是Assets文件夾,然后從那里向上冒泡,直到找到根目錄

var directory = new DirectoryInfo(Application.dataPath);
while(directory.GetDirectories(".git").Length == 0)
{
    directory = directory.Parent;

    if(directory == null)
    {
        throw new DirectoryNotFoundException("We went all the way up to the system root directory and didn't find any \".git\" directory!");
    }
}
var repositoryPath = directory.FullName;
var repositoryName = directory.Name;

你也可以從

var directory = new DirectoryInfo(System.Environment.CurrentDirectory);

這通常應該是你項目的根路徑,所以基本上已經比Assets高一個目錄。

假設您在運行游戲時指的是代碼的文件夾:

您可以按照此問題中所述使用AppDomain.CurrentDomain.BaseDirectory

暫無
暫無

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

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