سؤال

My app needs to write and maintain a log file and its not running in admin mode. My question is what path could my app write to in such a situation. How could I obtain that path ?

هل كانت مفيدة؟

المحلول

There are two good options:

  1. Use the Windows Event Log. You can easily create your own log for your application (if you expect to generate a lot of messages), or you can just add the messages to the standard logs (if you expect to generate only a few, occasional messages).

    Since this is a built-in feature, any technical person is going to know about it and be able to locate your log files easily. It's also very interoperable with centralized management systems.

  2. Write to a text file saved in the Application Data directory. This is where applications are supposed to store non-user data files, since, as you mentioned, the application directory is not something you can assume write privileges to.

    For a log file about stuff that is specific to a particular computer, I'd say that this is local (non-roaming) application data, so you want the Local App Data folder. I'm sure that there is a Qt wrapper for this, but in Win32, you would call the SHGetKnownFolderPath function, specifying the KNOWNFOLDERID value FOLDERID_LocalAppData.

    Remember that this function allocates memory to store the returned string—you must free it with a call to CoTaskMemFree when you are finished.

    Sample code:

    // Retrieve the path to the local App Data folder.
    wchar_t* pszPath = 0;
    SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &pszPath);
    
    // Make a copy of that path.
    std::wstring path(pszPath);
    
    // Free the memory now, so you don't forget!
    CoTaskMemFree(static_cast<void*>(pszPath));
    

نصائح أخرى

Refer to the SHGetKnownFolderPath API, probably using the FOLDERID_LocalAppData option.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top