1

I have a windows service which is running under "Local system". In "OnStart" method, I logged a event log entry using,

eventLog.WriteEntry($"Path: {Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), @"Pictures")}")

It logged as "Path: C:\WINDOWS\system32\config\systemprofile\Pictures"

But the same thing I tried in a Console App using Console.WriteLine, It logged "Path: C:\Users\john\Pictures"

In the windows service, I need to access the current user directory as I am getting from console app.

Please help me on this.

Amitava Karan
  • 637
  • 1
  • 5
  • 20
  • 1
    The current user of the service _is_ the Local System. It's not running under the user account 'john', but under the special system account 'Local System'. If you want the windows service to run as the user 'john', you can specify that when you install the service, or change it in the 'services' utility in windows. – gnud Jan 05 '20 at 11:48
  • I do not suppose that windows service has to interact with user profiles. What are you trying to achieve ? Do you want to work with current logged user pictures? Then you can try to [subscribe for SessionChange event](https://stackoverflow.com/questions/30759933/windows-service-get-username-when-user-log-on) and try to get the logged in user profile. Please note that more than one user can be logged in – oleksa Jan 05 '20 at 11:53
  • Basically I am building a windows service which will upload images/pictures from a specific folder say "test" under picture library "C:\Users\john\Pictures" to azure blob storage. I am using FileSystemWatcher to be notified of new file and start the upload. I am assuming there is only one user in the system. – Amitava Karan Jan 05 '20 at 12:16
  • Assume all you want, but Windows is multi-user and multi-session (e.g. RDP sessions, fast user switching, runas logons, scheduled tasks, over-the-shoulder UAC), so how are you determining which user's profile to use here? Is this servicing a request sent over a named pipe? In that case you can make the current thread impersonate the requesting client; look up the .NET equivalent of `ImpersonateNamedPipeClient`. – Eryk Sun Jan 05 '20 at 12:26
  • you can use the [OnSessionChange](https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicebase.onsessionchange?view=netframework-4.8) to get current user session and local user profile path. However service will work only if there is logged in user and do nothing otherwise. The idea of windows service is to work even if there is no user logged in however. You can change to the [system tray application (to simplify the development)](https://stackoverflow.com/questions/995195/how-can-i-make-a-net-windows-forms-application-that-only-runs-in-the-system-tra) – oleksa Jan 05 '20 at 16:52
  • I try out the system tray approach, thanks! – Amitava Karan Jan 08 '20 at 06:55

0 Answers0