.net 如何在 WindowsC++/CLI 中获取应用程序可执行文件名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/124886/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:03:28  来源:igfitidea点击:

How to get the application executable name in WindowsC++/CLI?

.netwinapic++-cli

提问by Daemin

I need to change the functionality of an application based on the executable name. Nothing huge, just changing strings that are displayed and some internal identifiers. The application is written in a mixture of native and .Net C++-CLI code.

我需要根据可执行文件名称更改应用程序的功能。没什么大不了的,只是改变显示的字符串和一些内部标识符。该应用程序混合使用本机和 .Net C++-CLI 代码编写。

Two ways that I have looked at are to parse the GetCommandLine() function in Win32 and stuffing around with the AppDomain and other things in .Net. However using GetCommandLine won't always work as when run from the debugger the command line is empty. And the .Net AppDomain stuff seems to require a lot of stuffing around.

我看过的两种方法是在 Win32 中解析 GetCommandLine() 函数,并在 .Net 中填充 AppDomain 和其他东西。但是,当从调试器运行时,命令行为空时,使用 GetCommandLine 并不总是有效。.Net AppDomain 的东西似乎需要很多东西。

So what is the nicest/simplest/most efficient way of determining the executable name in C++/CLI? (I'm kind of hoping that I've just missed something simple that is available in .Net.)

那么在 C++/CLI 中确定可执行文件名称的最好/最简单/最有效的方法是什么?(我有点希望我刚刚错过了 .Net 中可用的一些简单的东西。)

Edit: One thing that I should mention is that this is a Windows GUI application using C++/CLI, therefore there's no access to the traditional C style main function, it uses the Windows WinMain() function.

编辑:我应该提到的一件事是,这是一个使用 C++/CLI 的 Windows GUI 应用程序,因此无法访问传统的 C 风格主函数,它使用 Windows WinMain() 函数。

回答by Ferruccio

Call GetModuleFileName()using 0 as a module handle.

使用 0 作为模块句柄调用GetModuleFileName()

Note: you canalso use the argv[0] parameter to main or call GetCommandLine() if there is no main. However, keep in mind that these methods will not necessarily give you the complete path to the executable file. They will give back the same string of characters that was used to start the program. Calling GetModuleFileName() will always give you a complete path & file name.

注意:如果没有 main,您可以使用 argv[0] 参数来 main 或调用 GetCommandLine()。但是,请记住,这些方法不一定会为您提供可执行文件的完整路径。他们将返回用于启动程序的相同字符串。调用 GetModuleFileName() 将始终为您提供完整的路径和文件名。

回答by Adam Pierce

Ferruccio's answer is good. Here's some example code:

费鲁乔的回答很好。下面是一些示例代码:

TCHAR exepath[MAX_PATH+1];

if(0 == GetModuleFileName(0, exepath, MAX_PATH+1))
    MessageBox(_T("Error!"));

MessageBox(exepath, _T("My executable name"));

回答by Adam Pierce

Use the argvargument to main:

使用argv参数main

int main(int argc, char* argv[])
{
    printf("%s\n", argv[0]);  //argv[0] will contain the name of the app.
    return 0;
}

You may need to scan the string to remove directory information and/or extensions, but the name will be there.

您可能需要扫描字符串以删除目录信息和/或扩展名,但名称将在那里。

回答by computinglife

Use __argv[0]

使用 __argv[0]

回答by Brian ONeil

There is a static Method on Assembly that will get it for you in .NET.

Assembly 上有一个静态方法,可以在 .NET 中为您获取它。

Assembly.GetEntryAssembly().FullName

Edit: I didn't realize that you wanted the file name... you can also get that by calling:

编辑:我没有意识到你想要文件名......你也可以通过调用:

Assembly.GetEntryAssembly().CodeBase

That will get you the full path to the assembly (including the file name).

这将为您提供程序集的完整路径(包括文件名)。

回答by ingconti

I can confirm it works under win64/visual studio 2017/ MFC

我可以确认它在 win64/visual studio 2017/MFC 下工作

TCHAR szFileName[MAX_PATH + 1];
GetModuleFileName(NULL, szFileName, MAX_PATH + 1);
auto exe = CString(szFileName);

exe contains full path to exe.

exe 包含 exe 的完整路径。