[英]Is there a simple Program Files/Program Files (x86) directive for C++ in windows?
I am currently hard-coding the path to my application as follows: 我目前正在硬编码我的应用程序的路径如下:
const char* OriginCopyFile = "C:\\Program Files (x86)\\i-cut\\i-cut\\Origin_copy.txt";
This application is going to be running in both 32 and 64 systems. 该应用程序将在32和64系统中运行。 How can I detect the path without the file name in order to reuse it with several files and make it portable between architecture.
如何在没有文件名的情况下检测路径,以便将其与多个文件一起使用,并使其在架构之间可移植。
You can use GetModuleFileName to get the path to your executable, wherever it was installed or even moved later. 您可以使用GetModuleFileName获取可执行文件的路径,无论它在何处安装,甚至以后移动。 You can then PathRemoveFileSpec to remove the executable name (or
strchr()
and friends if you want to support earlier versions than Windows 2000). 然后,如果要支持早于Windows 2000的版本,则可以使用PathRemoveFileSpec删除可执行文件名(或
strchr()
和朋友)。
SHGetSpecialFolderPath(CSIDL_PROGRAM_FILES)
will at least give the path to the program files directory. SHGetSpecialFolderPath(CSIDL_PROGRAM_FILES)
至少会给出程序文件目录的路径。 You'll have to deal with adding the rest of the path and file name. 您将不得不处理添加其余路径和文件名。
You can use environment variables for this: 您可以使用环境变量:
#include <stdio.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
char* programFiles = getenv("ProgramFiles(x86)");
if (programFiles==NULL)
{
programFiles = getenv("ProgramFiles");
}
printf(programFiles);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.