9

Typically (in Windows 7), installing a program will ask for permission to modify the system. As an administrator, I can give the authorization without supplying a password.

I'm trying to figure out how to take an administrator action (restart IIS) from C# code running as a user who is AN administrator, but the not THE "Administrator" account.

Fantius
  • 3,806
  • 5
  • 32
  • 49

6 Answers6

5

To run a process as elevated you can use the runas verb.

Process elevated = new Process();
elevated.StartInfo.Verb = "runas";
elevated.StartInfo.FileName = "Whatever.exe";
elevated.Start();

For restarting IIS (as mentioned before) use iisreset.

Hope you find this useful.

ZFE
  • 301
  • 2
  • 8
  • In a command prompt, when I type "runas /user:Administrator iisreset", it asks me for a password, even though the user who launched the command prompt is an administrator. I should not need a password. – Fantius Nov 03 '11 at 23:07
  • I don't think that the runas command is exactly the same as the runas verb. From the question I assumed that You would like to do this from code. – ZFE Jan 03 '12 at 08:15
4

For anyone still looking for this, here is code that I use to help me out with this.

    private static void DoIISReset()
    {
        Process iisReset = new Process();
        iisReset.StartInfo.FileName = "iisreset.exe";
        iisReset.StartInfo.RedirectStandardOutput = true;
        iisReset.StartInfo.UseShellExecute = false;
        iisReset.Start();
        iisReset.WaitForExit();
    }

Hope this helps!

Pieter
  • 41
  • 4
3

Try to execute the IISReset command from C#

http://technet.microsoft.com/en-us/library/cc758159(WS.10).aspx

iisreset /noforce

Using ProcessStart

System.Diagnostics.Process.Start(@"C:\Windows\System32\iisreset.exe");

If you're using AD Authentication and you're an administrator this should work

hunter
  • 62,308
  • 19
  • 113
  • 113
2
System.Diagnostics.Process.Start(@"C:\Windows\System32\iisreset.exe");

This code help to you but you can get Access Denied.

For you to not get Access Denied:

  1. Right Click Project
  2. Add New İtem
  3. Add Application Manifest File
  4. Change this section

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

To this

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Kenya
  • 9
  • 1
  • 3
Mehmet Topçu
  • 1
  • 1
  • 16
  • 31
0

There are two ways to do this but fr both you need to run VS as administration.

  1. This code will prompt in an empty cmd for some time and will close the window automatically.

    Process iisReset = new Process(); iisReset.StartInfo.FileName = "iisreset.exe"; iisReset.StartInfo.RedirectStandardOutput = true; iisReset.StartInfo.UseShellExecute = false; iisReset.Start(); iisReset.WaitForExit();

    1. this code will also restart IIS and it will prompt CMD with few processing.

      Process.Start(@"C:\WINDOWS\system32\iisreset.exe", "/noforce");

sunm
  • 1
  • 2
-1

Here is a link to how this is done in power shell http://www.computerperformance.co.uk/powershell/powershell_service_start.htm

Another possibility would be to use WMI http://www.motobit.com/tips/detpg_vbs-wmi-restart-service/

Here is another way directly in # http://www.csharp-examples.net/restart-windows-service/

I hope this helps....

Nigel Findlater
  • 1,684
  • 14
  • 34
  • This does not answer the question around the requirement of doing it in C# – Dave Friedel Jan 30 '19 at 06:59
  • @Dave Friedel, The last link has C# examples how to restart a service. So it does answer the question – Nigel Findlater Jan 31 '19 at 08:46
  • 1
    I'll remove the down vote, but you do not provide the specifics for restarting IIS with a link to a remote site that doesn't answer the question. To provide value, some pseudo code outlining the action would be more useful to the community. - Update - my vote is locked unless the answer is edited. Plus it would lead them down other issues like the link here: https://stackoverflow.com/questions/19763527/why-my-c-sharp-does-not-have-system-serviceprocess-library – Dave Friedel Jan 31 '19 at 09:20