1

What is wrong with the following code?

        ProcessStartInfo startInfo = default(ProcessStartInfo);

        startInfo = new ProcessStartInfo(SetupProgramPath)
        {
            UseShellExecute = true,
            Verb = "runas",
            WindowStyle = ProcessWindowStyle.Normal,
            CreateNoWindow = false
        };

        Process.Start(startInfo); 

It is expected to prompt for credentials but nothing shows up. The system has the UAC enabled and not supposed to be changed. I appreciate your help in this one. Thank you in advance.

Sunder
  • 129
  • 1
  • 4
  • 13

2 Answers2

7

I have worked this out with the following code

ProcessStartInfo startInfo = default(ProcessStartInfo);

startInfo = new ProcessStartInfo(SetupProgramPath)
{
    UseShellExecute = true,
    Verb = "runas",
    WindowStyle = ProcessWindowStyle.Normal,
    FileName = "msiexec",
    Arguments = "/i \"" + SetupProgramPath + "\"",
    CreateNoWindow = false
};

Process.Start(startInfo); 
Sunder
  • 129
  • 1
  • 4
  • 13
0

If you want to ask the user to enter credentials of a different user, then use "runasuser":

ProcessStartInfo startInfo = new ProcessStartInfo(SetupProgramPath)
{
    UseShellExecute = true,
    Verb = "runasuser",
    WindowStyle = ProcessWindowStyle.Normal,
    CreateNoWindow = false
};

Process.Start(startInfo); 
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • They're trying to run as admin, not a user – TheLethalCoder Jan 20 '17 at 16:15
  • Thank you for your help. Unfortunately, it did not work. It neither prompted for credentials nor launched the setup program. My setup program resides in the network – Sunder Jan 20 '17 at 16:26
  • I suppose that the value of "SetupProgramPath" is wrong. Just try to use a local application first like "calc.exe". Then try to use the path to your setup application as a string literal. If this works, then try to fix the code that fills "SetupProgramPath" – NineBerry Jan 20 '17 at 16:42
  • Sorry to bother you. It works just perfect when I disable UAC and remove Verb = "runasuser" or Verb = "runas". The problem now is to run program with UAC enabled. I'm able to access the path without error. – Sunder Jan 20 '17 at 17:00
  • Does it work with "calc.exe" or "regedit.exe"? If yes, then tell us what is special about the SetupProgram. If not, show us a complete and minimal example, because this works over here. – NineBerry Jan 20 '17 at 17:08
  • I have tested this with calc.exe with Verb as both runas and runasuser. It works just perfect. runas not prompted me for the credential whereas runasuser did. The user is a local administrator. I'm wondering what seems to be the problem with the application. The SetupProgramPath is the installation file containing the complete path. – Sunder Jan 20 '17 at 17:43
  • Try running the application from a local disc instead of from a network share – NineBerry Jan 20 '17 at 17:57
  • Will try but the .msi file will usually be in the network shared path for me – Sunder Jan 20 '17 at 18:04
  • Do you use a drive letter mapped to the network location like z:\myapp.exe or an unc path like \\myserver\myapp.exe? – NineBerry Jan 20 '17 at 20:38
  • It is an unc path \\myserver\myapp.msi – Sunder Jan 21 '17 at 04:25