9

There is "Setup project" in VS. During installation I launch another process:

System.Diagnostics.Process process = new System.Diagnostics.Process();
//fill StartInfo and run call Start()
process.Start();

If I run installer under Windows 7 and install for "Everyone", process start under the SYSTEM. If I install "Just for me", process start under Current user. How do I always start process under Current user?

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
alxndr
  • 2,349
  • 3
  • 17
  • 14

3 Answers3

7

I have found very simple solution. All that you need it just create a new class and copy text from this link.

To launch the process call ProcessAsUser.Launch("program name");

alxndr
  • 2,349
  • 3
  • 17
  • 14
  • I've been searching a way to run my windows app for the current user via a windows service on a timer for a long time. And this worked out of the box. `ProcessAsUser.Launch("https://www.youtube.com/watch?v=dQw4w9WgXcQ");` – Jean-Philippe Bergeron May 14 '19 at 17:23
  • 2
    but it need username and password and we don't have that – Dr. Rajesh Rolen Jul 11 '19 at 12:01
  • But this will only be able to run on Windows, not Linux distributions... – Pieterjan Jul 08 '22 at 05:46
  • @Dr.RajeshRolen When running with SYSTEM privileges, password is not necessary. The username is by default taken from the logged in user that has a running explorer.exe process. – Benjamin Jan 09 '23 at 17:27
1

I had a similar problem: My setup extension (custom action) needed Admin privileges which brought up an elevation box. After I start my application at the end of "Just for Me" the process had settings that were made for the admin context. For example my user account likes to see all extensions of files in Windows Explorer but the admin account was configured to hide them. So in every file open box I couldn't see the extensions. To cure this this piece of code worked:

ProcessStartInfo startInfo = new ProcessStartInfo(ShortcutTarget);
startInfo.LoadUserProfile = true;
startInfo.UseShellExecute = false;
Process.Start(startInfo);

It works only in "Just for Me" mode, in "Everyone" the admin's settings are used. But this is ok for me.

-1

Use ProcessStartInfo class and its property UserName, then use it as argument for Process.Start static method.

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Process.Start(startInfo);
Tomek
  • 3,267
  • 2
  • 22
  • 23
  • I have tried it before, but I got next error message: "The stud received bad data". And I do not know how to fix this issue. – alxndr Mar 01 '12 at 08:00
  • what about the password? this wont work without it :) – John Mar 15 '16 at 10:39