vb.net 如何获取.EXE的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1460684/
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
How to get file name of .EXE
提问by aphoria
Possible Duplicates:
Getting the path of the current assembly
C#: How do I get the path of the assembly the code is in?
可能的重复项:
获取当前程序集的路径
C#:如何获取代码所在程序集的路径?
Using VB 2008, how can I get the file name of a running .EXE from inside itself?
使用 VB 2008,如何从内部获取正在运行的 .EXE 的文件名?
EDIT: This is for a console app, so Application.ExecutablePath
will not work.
编辑:这是一个控制台应用程序,所以Application.ExecutablePath
不会工作。
回答by RRUZ
There are a few ways:
有几种方法:
Application.ExecutablePath
or
或者
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
or
或者
System.Reflection.Assembly.GetExecutingAssembly().Location
回答by Blue Toque
This has been answered before.
From anywhere in your code you could be in an assembly that was loaded by the originating EXE. You also may not have a reference to the Application singleton, so using the Assembly class is your best bet.
从代码中的任何位置,您都可以位于由原始 EXE 加载的程序集中。您也可能没有对 Application 单例的引用,因此使用 Assembly 类是您最好的选择。
Safest way is Assembly.GetEntryAssembly().Location gets the location on the filesystem where the Assembly is currently. If it is shadow copiedthen this is the Shadow-copy location. If it is click-once deployed, then this is a crazy path to a file in the sandboxarea.
最安全的方法是Assembly.GetEntryAssembly().Location 获取文件系统上程序集当前所在的位置。如果是卷影复制,则这是卷影复制位置。如果它是单击一次部署,那么这是沙箱区域中文件的疯狂路径。
The original location of the assembly will be at Assembly.GetEntryAssembly().Codebase
程序集的原始位置将在Assembly.GetEntryAssembly().Codebase
回答by Steven Sudit
Process.GetCurrentProcess().MainModule
Process.GetCurrentProcess().MainModule
edit
编辑
Another way might be to use Environment.GetCommandLineArgs()[0], but I prefer using Process.
另一种方法可能是使用 Environment.GetCommandLineArgs()[0],但我更喜欢使用 Process。
回答by Rowland Shaw
You should find it in the property: Application.ExecutablePath
您应该在该属性中找到它: Application.ExecutablePath