如何使用 C# 检索我所在的存储库目录名称?

发布于 2025-01-11 03:54:00 字数 288 浏览 0 评论 0原文

所以我目前正在特定存储库中使用 Unity 项目。如何获取我正在使用的存储库的名称?即 .git 文件夹正上方的文件夹名称。

我这样做的方式是这样的:

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

但是,这感觉不是最好的方式,因为它几乎就像我在硬编码一样?

我还可以通过哪些其他方法来做到这一点?

So I am currently working using a Unity project in a specific repository. How can I get the name of this repository I am working in? That is, the name of the folder directly above the .git folder.

The way I have done it is like so:

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

However, this feels like not the best way to do it since it's almost like as if I am hard coding?

What other ways can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2025-01-18 03:54:00

您可能可以从一个已知的文件夹开始,例如 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);

它开始,通常应该是项目的根路径,所以基本上已经比这资产

You could probably start with a known folder like e.g. Application.dataPath which is the Assets folder and from there bubble up until you find the root

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;

You could pobably also start at

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

which usually should be the root path of your project, so basically already one directory higher than the Assets.

擦肩而过的背影 2025-01-18 03:54:00

假设您指的是运行游戏时代码的文件夹:

您可以按照此 问题

Assuming you mean the folder of your code when you run your game:

You can use AppDomain.CurrentDomain.BaseDirectory as stated in this question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文