Writing config file in C:\Program Files (x86)\MyApp\myapp.cfg, vs. Administrator privilege

前端 未结 1 1483
独厮守ぢ
独厮守ぢ 2021-02-11 06:49

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 o

1条回答
  •  情书的邮戳
    2021-02-11 07:16

    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 \ 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

    0 讨论(0)
提交回复
热议问题