如何使用 C# 检索我所在的存储库目录名称?
所以我目前正在特定存储库中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能可以从一个已知的文件夹开始,例如
Application.dataPath
这是Assets
文件夹,从那里向上冒泡,直到找到根目录您也可以从
它开始,通常应该是项目的根路径,所以基本上已经比这
资产
。You could probably start with a known folder like e.g.
Application.dataPath
which is theAssets
folder and from there bubble up until you find the rootYou could pobably also start at
which usually should be the root path of your project, so basically already one directory higher than the
Assets
.假设您指的是运行游戏时代码的文件夹:
您可以按照此 问题
Assuming you mean the folder of your code when you run your game:
You can use
AppDomain.CurrentDomain.BaseDirectory
as stated in this question