在 vb.net 控制台 exe 中获取启动路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/300578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the startup path in a vb.net console exe
提问by wusher
How do get the startup path ( system.windows.forms.application.StartupPath ) of my exe without adding a reference to system.windows.forms?
如何在不添加对 system.windows.forms 的引用的情况下获取我的 exe 的启动路径( system.windows.forms.application.StartupPath )?
回答by JamesSugrue
You could try
你可以试试
System.AppDomain.CurrentDomain.BaseDirectory
which would work for most cases.
这适用于大多数情况。
回答by Mitch Wheat
EDIT: @KiwiBastard's answer is the correct method:
编辑:@KiwiBastard 的答案是正确的方法:
System.AppDomain.CurrentDomain.BaseDirectory
Add a reference to System.Reflection
and use
添加引用System.Reflection
并使用
Assembly.GetExecutingAssembly().Location
EDIT: Depending where you intend getting the startup path, this might be more appropriate:
编辑:根据您打算在哪里获取启动路径,这可能更合适:
Assembly.GetEntryAssembly().Location
回答by Andrew Moore
You can get the startup path without reflection by using:
您可以使用以下方法获取无需反射的启动路径:
IO.Path.GetDirectoryName(Diagnostics.Process.GetCurrentProcess().MainModule.FileName)