0

I am creating a service that access a folder path:

string localAppDataFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

Unfortunately, the folder returned is not:

C:\Users\mainuser\AppData\Local

... but:

C:\Windows\system32\config\systemprofile\AppData\Local

...instead.

mainuser is the user that is currently logged in. Is it possible for service to launch a program that is placed in current users AppData\Local? I am aware that you can go to Services-> Right click Properties-> Log on and type in password and account name, but I am looking for a programmatic solution, ideally using sc create command.

Alternatively, I can move all folders in interest to C:\ProgramData and completely avoid current user. Is there a way to keep the files in users AppData\Local and use programmatic solution without password?

Robert Segdewick
  • 543
  • 5
  • 17
  • Your Windows Service might be able to start a child process running in a different account by specifying the `Username`/`Password` in `ProcessStartInfo` –  Mar 12 '20 at 22:37
  • For a windows service, there is no concept of a "current user". As your windows service is running, there can be multiple users logged in to the system (RDP) or there can be no logon at all. Does your service assume the existence of a logged-on user? – Oguz Ozgul Mar 12 '20 at 22:42
  • @OguzOzgul yeah, it should only run when a particular user logs in. – Robert Segdewick Mar 12 '20 at 22:44
  • @MickyD I don't know the username/password. I am relying in windoes to find the correct user. – Robert Segdewick Mar 12 '20 at 22:50
  • Having a windows service to execute an executable file that is included in it's installer (therefore known where to find) might be a better design. You should not try to access any user's (private) AppData folder which is probably restricted by the OS anyway. – Oguz Ozgul Mar 12 '20 at 23:14
  • It sounds like you just should use Windows Task Scheduler. You can have it run a task when a certain user logs in; unlocks their workstation or other trigger. You can specify to run the task in their account; elevated or other –  Mar 13 '20 at 00:07
  • I tested Task Scheduler vs Service execution. For reasons unknown to me, task scheduler scheduled at log on, executes later than the service. My goal is to execute a service before any user program launches. – Robert Segdewick Mar 13 '20 at 08:56

1 Answers1

0

In short, Services don't (and shouldn't) launch programs for users or access user information in general (unless they run as the specified user). They are agnostic of users and their profiles, their use-case is to run as part of the system

If you want to run an application when a user logs on, either use a group policy, or add it to the user settings to run on startup. If need be, set this up in the installer

This is the way every service works, and the norm for windows for a long time (with the exception of drivers)

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • If I use the Task Scheduler set at "Log On", it happens too late. I am trying to trigger a program before user domain programs (Discord, Chrome etc) are autostarted. Can you give me some google search terms on group policy and how to set user settings to run at startup. I am shifting from mac to windows so I ma having trouble coining the right terms to search for. Currently I do it with nsis installer by running a variation of exec command using 'sc create name binpath="" start="auto" ' – Robert Segdewick Mar 13 '20 at 09:08