简体   繁体   中英

How do i reset IIS remotely using C#.Net code?

I have the IIS service running on Windows Server 2008 and i want to reset it from my windows 7 machine .

How do i reset IIS remotely using C#.Net code?

I tried using Microsoft.Web.Administration, but it doesn't accept remote server details to connect to .Any other API's that can be used for this purpose ?

If you have a specific web site name, you can use the WMI to stop and start that web site. If it's IIS as a whole, I'm sure there is something in the IIS WMI provider for doing this. Here I'm using WebAdministration to manage a web site under IIS, but there's also MicrosoftIISV2. Google for WmiExplorer, there are some good ones out there.

        var connOptions = new ConnectionOptions();
        connOptions.Authentication = AuthenticationLevel.PacketPrivacy;
        // if you want to connect as someone other than logged in user
        //connOptions.Username = username;
        //connOptions.Password = password;

        var scope = new ManagementScope("\\localhost\WebAdministration", connOptions);

        WqlObjectQuery query = new WqlObjectQuery(`enter code here`string.Format("SELECT * FROM Site WHERE Name = '{0}'", "Default Web Site"));

        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
        {
            foreach (ManagementObject site in searcher.Get())
            {
                ManagementBaseObject inParams = site.GetMethodParameters("Stop");
                site.InvokeMethod("Stop", inParams, null);

                ManagementBaseObject inParams2 = site.GetMethodParameters("Start");
                site.InvokeMethod("Start", inParams2, null);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM