How to fetch Program files in Windows
-
I am developing an application which has to copy certain folders in the Program Files location where the application will be installed. I have looked into QStandardPaths but unable to find any variable for Program files in windows. Looking into forum and googling always gives me ApplicationLocation or data location which are not C/D:Program Files.
getenv is also deprecated which doesnt serve the purpose. Can we somehow fetch Program files in Windows using variable?
-
I am developing an application which has to copy certain folders in the Program Files location where the application will be installed. I have looked into QStandardPaths but unable to find any variable for Program files in windows. Looking into forum and googling always gives me ApplicationLocation or data location which are not C/D:Program Files.
getenv is also deprecated which doesnt serve the purpose. Can we somehow fetch Program files in Windows using variable?
Hi,
Can you explain why you need to copy these files ?
In any case, doesn't QCoreApplication::applicationDirPath do what you want ?
-
I am developing an application which has to copy certain folders in the Program Files location where the application will be installed. I have looked into QStandardPaths but unable to find any variable for Program files in windows. Looking into forum and googling always gives me ApplicationLocation or data location which are not C/D:Program Files.
getenv is also deprecated which doesnt serve the purpose. Can we somehow fetch Program files in Windows using variable?
Hi @supratik123,
Looking into forum and googling always gives me ApplicationLocation or data location which are not C/D:Program Files.
That is correct. As per the docs,
ApplicationsLocation
will yield something like"C:/Users/<USER>/AppData/Roaming/Microsoft/Windows/Start Menu/Programs"
on Windows (which is a bit odd, if you ask me). And the*DataLocation
will be user data paths, not "Program Files".getenv is also deprecated which doesnt serve the purpose.
Actually, getenv() is only deprecated because that ancient version not threadsafe, and returns a pointer to table entry should not be modified. There's nothing wrong with reading the
programfiles
(andprogramfiles(86)
) environment variables, its just strongly recommended (and for good reason) that you use something safer, eg getenv_s() or _wgetenv_s() on Windows, or secure_getenv() on Linux.Personally, I would use Qt's qgetenv() or qEnvironmentVariable() functions, which, on Windows, wrap
getenv_s()
and_wgetenv_s()
respectively. You can see how they do it here: qtenvironmentvariables.cpp.Cheers.
-
Hi @supratik123,
Looking into forum and googling always gives me ApplicationLocation or data location which are not C/D:Program Files.
That is correct. As per the docs,
ApplicationsLocation
will yield something like"C:/Users/<USER>/AppData/Roaming/Microsoft/Windows/Start Menu/Programs"
on Windows (which is a bit odd, if you ask me). And the*DataLocation
will be user data paths, not "Program Files".getenv is also deprecated which doesnt serve the purpose.
Actually, getenv() is only deprecated because that ancient version not threadsafe, and returns a pointer to table entry should not be modified. There's nothing wrong with reading the
programfiles
(andprogramfiles(86)
) environment variables, its just strongly recommended (and for good reason) that you use something safer, eg getenv_s() or _wgetenv_s() on Windows, or secure_getenv() on Linux.Personally, I would use Qt's qgetenv() or qEnvironmentVariable() functions, which, on Windows, wrap
getenv_s()
and_wgetenv_s()
respectively. You can see how they do it here: qtenvironmentvariables.cpp.Cheers.
@Paul-Colby
As you say,ApplicationsLocation
returning.../Start Menu/Programs
seems a bit "odd". It is "surprising" that Qt does not offer anything to returnProgram Files
etc. under Windows. Yes, these are very Windows-specific, and Qt is largely (but not totally) platform-agnostic, but still I would have guessed Qt would offer something for this. But it is what it is, and code can use environment variables or native Windows calls to get the desired path.@supratik123
Don't forget to verify whether you are looking forProgram Files
orProgram Files (x86)
, they are not the same place!