Search code examples
c#.netwindows-servicesuser-accountslocal-system-account

Finding users' HOMEPATHs from a service


In the process of writing a service I have

        //# Service Account Information
        serviceProcessInstaller.Account = ServiceAccount.LocalSystem;

in the installer for it.

I was having problems writing a log to

    static string USERS_HOME_DIR = Environment.GetEnvironmentVariable("HOMEDRIVE") + Environment.GetEnvironmentVariable("HOMEPATH");

since when the service was running (installed "as administrator") the event logger was reporting

Service cannot be started. System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Documents\Folder of Interest'

I need the HOMEPATHs of the users of the local_PC, any ideas how to get them?

UPDATE

Actually it would be better to just get the path for the currently logged on user, as their session and the service start. My sevice is not re-entrant but one user is better than none.


Solution

  • If I understand your question correctly what you're looking for are the special folder enumerations.

    With something like:

    String PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    

    If you want to run the service as the localsystem account there's a separate set of standards for storing the data. See this answer on serverfault.