Question

I have made an installer (using Inno Setup) for my software, which is finally installed in

C:\Program Files (x86)\MyApp\myapp.exe

as usual on Windows platform, and it works perfectly.

A problem comes when my software tries to write its config file here :

C:\Program Files (x86)\MyApp\myapp.cfg

here a Permission denied error arrives !

The only solution I have found is "Run software as administrator", but I don't like this solution : I don't want to ask the final user to have to do "Run as adminstrator" each time they will use the software.

Is there a clean solution for having the right to write in C:\Program Files (x86)\MyApp\myapp.cfg without asking the final user to "run as administrator" ?

Was it helpful?

Solution

As @TLama said, never ever try to write to the application folder. That was never the recommended practice and since Vista Microsoft has disabled it (by first allowing you to write but actually redirecting the writing to another directory, a functionality which in later OS versions seems to have been removed - and good thing too as it was making support hellish).

To retrieve a known folder path, use SHGetFolderPath function. Depending on whether the application data is shared among all users or just used by the user running the app, you should call SHGetFolderPath either with CSIDL_COMMON_APPDATA or with CSIDL_LOCAL_APPDATA respectively. Avoid writing directly in those folders but create subfolders like <your company>\<your app> and use those instead.

Note that SHGetFolderPath has been deprecated and SHGetKnownFolderPath should be used in lieu of it but only if you don't need compatibility with Windows versions prior to Vista.

For completeness sake, this thread also describes how to import SHGetFolderPath into Python.

Update: Corrected to avoid using environment variables and SHGetFolderPath instead per comments by @RemyLebeau @TLama

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top