首页 > C# > 如何在 .NET 控制台应用程序中获取应用程序的路径?

如何在 .NET 控制台应用程序中获取应用程序的路径?

上一篇 下一篇

如何在控制台应用程序中查找应用程序的路径?

在 Windows 窗体中,我可以使用 查找当前路径,但这似乎在控制台应用程序中不可用。Application.StartupPath

网友回答:

可以使用以下代码获取当前应用程序目录。

AppDomain.CurrentDomain.BaseDirectory

网友回答:

您有两个选项来查找应用程序的目录,您选择的目录将取决于您的用途。

// to get the location the assembly is executing from
//(not necessarily where the it normally resides on disk)
// in the case of the using shadow copies, for instance in NUnit tests, 
// this will be in a temp directory.
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

//To get the location the assembly normally resides on disk or the install directory
string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

//once you have the path you get the directory with:
var directory = System.IO.Path.GetDirectoryName(path);

网友回答:

System.Reflection.Assembly.GetExecutingAssembly().1Location

如果您只需要目录,请将其与System.IO.Path.GetDirectoryName

1根据 Mr.Mindor 的评论:
返回执行程序集当前所在的位置,该位置可能是也可能不是未执行时程序集所在的位置。对于卷影复制程序集,您将在临时目录中获得一个路径。 将返回程序集的“永久”路径。
System.Reflection.Assembly.GetExecutingAssembly().LocationSystem.Reflection.Assembly.GetExecutingAssembly().CodeBase

模板简介:该模板名称为【如何在 .NET 控制台应用程序中获取应用程序的路径?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【C#】栏目查找您需要的精美模板。

相关搜索
  • 下载密码 lanrenmb
  • 下载次数 241次
  • 使用软件 Sublime/Dreamweaver/HBuilder
  • 文件格式 编程语言
  • 文件大小 暂无信息
  • 上传时间 02-21
  • 作者 网友投稿
  • 肖像权 人物画像及字体仅供参考
栏目分类 更多 >
热门推荐 更多 >
html5 微信模板 企业网站 微信素材 微信公众平台 自适应 微信文章 单页式简历模板 微信图片 响应式
您可能会喜欢的其他模板