如何用C#操纵IIS创建iis应用程序池设置

C# 操作IIS方法集合_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
C# 操作IIS方法集合
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩4页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢C#中如何默认以管理员身份运行程序 - C#教程 - 编程入门网
C#中如何默认以管理员身份运行程序
一、通过配置文件实现以管理员身份运行程序
Vista 和 Windows 7 操作系统为了加强安全,增加了 UAC(用户账户控制) 的机制,如果 UAC 被打开,用户即使是以管理员权限登录,其应用程序默认情况下也无法对系统目录,系统注册表等可能影响系统运行的设置进行写操作。这个机制大大增强了系统的安全性,但对应用程序开发者来说,我们不能强迫用户去关闭UAC,但有时我们开发的应用程序又需要以 Administrator 的方式运行,即 Win7 中 以 as administrator 方式运行,那么我们怎么来实现这样的功能呢?
我们在 win7 下运行一些安装程序时,会发现首先弹出一个对话框,让用户确认是否同意允许这个程序改变你的计算机配置,但我们编写的应用程序默认是不会弹出这个提示的,也无法以管理员权限运行。本文介绍了 C# 程序如何设置来提示用户以管理员权限运行。
首先在项目中增加一个 Application Manifest File
默认的配置如下:
&?xml version=&1.0& encoding=&utf-8&?&
&asmv1:assembly manifestVersion=&1.0& xmlns=&urn:schemas-microsoft-com:asm.v1&
xmlns:asmv1=&urn:schemas-microsoft-com:asm.v1& xmlns:asmv2=&urn:schemas-microsoft-com:asm.v2&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&&
&assemblyIdentity version=&1.0.0.0& name=&MyApplication.app& /&
&trustInfo xmlns=&urn:schemas-microsoft-com:asm.v2&&
&security&
&requestedPrivileges xmlns=&urn:schemas-microsoft-com:asm.v3&&
&!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
&requestedExecutionLevel
level=&asInvoker& uiAccess=&false&
&requestedExecutionLevel
level=&requireAdministrator& uiAccess=&false&
&requestedExecutionLevel
level=&highestAvailable& uiAccess=&false&
If you want to utilize File and Registry Virtualization for backward
compatibility then delete the requestedExecutionLevel node.
&requestedExecutionLevel level=&asInvoker& uiAccess=&false&
&/requestedPrivileges&
&/security&
&/trustInfo&
&/asmv1:assembly&
URL:/Programming/csharp/84.htm随笔- 199&
评论- 832&
&&& Microsoft自Windows Vista一起发布了IIS 7.0,这个已经是去年的话题了,随后,由.NET开发的Web程序便逐步从IIS 6.0过渡到IIS 7.0上了。IIS 7.0提供了很多比上一版本更多的新特性,包括完全模块化的组件、文本文件的配置功能、MMC图形模式管理工具等等,并且与.NET编程语言结合得更加紧密了,在新添加的Microsoft.Web.Administration名称空间中也增加了很多用于管理和访问IIS的对象,从而使得通过编程方式操作IIS更加简便。虽然在IIS 6.0时代我们也可以非常轻松地通过C#来管理服务器的IIS,但相对来说,现在需要编写的代码更少,所能完成的功能更强。以下是我在曾经做的一个项目中所写的一个类库中的一部分,主要实现了对IIS 7.0的操作,包括创建和删除站点、创建和删除虚拟目录、创建和删除应用程序池、添加站点默认文档、判断站点和虚拟目录是否存在、以及检查Bindings信息等。
&&& 对于IIS 7.0的介绍读者如果有兴趣的话可以看看下面的两篇文章,我觉得不错!
&&& 不说废话了,赶紧贴代码吧。
&&& 首先是对站点的管理。我写了一个相对较为通用的私有方法,然后在对外的方法中给出了调用接口,包括了创建站点时应用程序池的创建和权限的管理。
CreateSite
///&&summary&
/// Create a new web site.
///&&/summary&
///&&param name="siteName"&&/param&
///&&param name="bindingInfo"&"*:&port&:&hostname&" &example&"*:"&/example&&/param&
///&&param name="physicalPath"&&/param&
public&static&void CreateSite(string siteName, string bindingInfo, string physicalPath)
&&& createSite(siteName, "http", bindingInfo, physicalPath, true, siteName +&"Pool", ProcessModelIdentityType.NetworkService, null, null, ManagedPipelineMode.Integrated, null);
private&static&void createSite(string siteName, string protocol, string bindingInformation, string physicalPath,
&&&&&&& bool createAppPool, string appPoolName, ProcessModelIdentityType identityType,
&&&&&&& string appPoolUserName, string appPoolPassword, ManagedPipelineMode appPoolPipelineMode, string managedRuntimeVersion)
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& Site site = mgr.Sites.Add(siteName, protocol, bindingInformation, physicalPath);
&&&&&&& // PROVISION APPPOOL IF NEEDED
&&&&&&& if (createAppPool)
&&&&&&&&&&& ApplicationPool pool = mgr.ApplicationPools.Add(appPoolName);
&&&&&&&&&&& if (pool.ProcessModel.IdentityType != identityType)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& pool.ProcessModel.IdentityType = identityT
&&&&&&&&&&& }
&&&&&&&&&&& if (!String.IsNullOrEmpty(appPoolUserName))
&&&&&&&&&&& {
&&&&&&&&&&&&&&& pool.ProcessModel.UserName = appPoolUserN
&&&&&&&&&&&&&&& pool.ProcessModel.Password = appPoolP
&&&&&&&&&&& }
&&&&&&&&&&& if (appPoolPipelineMode != pool.ManagedPipelineMode)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& pool.ManagedPipelineMode = appPoolPipelineM
&&&&&&&&&&& }
&&&&&&&&&&& site.Applications["/"].ApplicationPoolName = pool.N
&&&&&&& mitChanges();
&&&& 这个是删除站点的方法,比较简单。
DeleteSite
///&&summary&
/// Delete an existent web site.
///&&/summary&
///&&param name="siteName"&Site name.&/param&
public&static&void DeleteSite(string siteName)
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& Site site = mgr.Sites[siteName];
&&&&&&& if (site !=&null)
&&&&&&&&&&& mgr.Sites.Remove(site);
&&&&&&&&&&& mitChanges();
&&& 然后是对虚拟目录的操作,包括创建和删除虚拟目录,都比较简单。
CreateVDir
public&static&void CreateVDir(string siteName, string vDirName, string physicalPath)
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& Site site = mgr.Sites[siteName];
&&&&&&& if (site ==&null)
&&&&&&&&&&& throw&new ApplicationException(String.Format("Web site {0} does not exist", siteName));
&&&&&&& site.Applications.Add("/"&+ vDirName, physicalPath);
&&&&&&& mitChanges();
DeleteVDir
public&static&void DeleteVDir(string siteName, string vDirName)
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& Site site = mgr.Sites[siteName];
&&&&&&& if (site !=&null)
&&&&&&&&&&& Microsoft.Web.Administration.Application app = site.Applications["/"&+ vDirName];
&&&&&&&&&&& if (app !=&null)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& site.Applications.Remove(app);
&&&&&&&&&&&&&&& mitChanges();
&&&&&&&&&&& }
&&& 删除应用程序池。
DeletePool
///&&summary&
/// Delete an existent web site app pool.
///&&/summary&
///&&param name="appPoolName"&App pool name for deletion.&/param&
public&static&void DeletePool(string appPoolName)
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& ApplicationPool pool = mgr.ApplicationPools[appPoolName];
&&&&&&& if (pool !=&null)
&&&&&&&&&&& mgr.ApplicationPools.Remove(pool);
&&&&&&&&&&& mitChanges();
&&& 在站点上添加默认文档。
AddDefaultDocument
public&static&void AddDefaultDocument(string siteName, string defaultDocName)
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& Configuration cfg = mgr.GetWebConfiguration(siteName);
&&&&&&& ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
&&&&&&& ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
&&&&&&& ConfigurationElementCollection filesCollection = filesElement.GetCollection();
&&&&&&& foreach (ConfigurationElement elt in filesCollection)
&&&&&&&&&&& if (elt.Attributes["value"].Value.ToString() == defaultDocName)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& return;
&&&&&&&&&&& }
&&&&&&& try
&&&&&&&&&&& ConfigurationElement docElement = filesCollection.CreateElement();
&&&&&&&&&&& docElement.SetAttributeValue("value", defaultDocName);
&&&&&&&&&&& filesCollection.Add(docElement);
&&&&&&& catch (Exception) { }&& //this will fail if existing
&&&&&&& mitChanges();
&&&& 检查虚拟目录是否存在。
VerifyVirtualPathIsExist
public&static&bool VerifyVirtualPathIsExist(string siteName, string path)
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& Site site = mgr.Sites[siteName];
&&&&&&& if (site !=&null)
&&&&&&&&&&& foreach (Microsoft.Web.Administration.Application app in site.Applications)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& if (app.Path.ToUpper().Equals(path.ToUpper()))
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& return&true;
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&& return&false;
&&&& 检查站点是否存在。
VerifyWebSiteIsExist
public&static&bool VerifyWebSiteIsExist(string siteName)
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& for (int i =&<span style="color: #; i & mgr.Sites.C i++)
&&&&&&&&&&& if (mgr.Sites[i].Name.ToUpper().Equals(siteName.ToUpper()))
&&&&&&&&&&& {
&&&&&&&&&&&&&&& return&true;
&&&&&&&&&&& }
&&& return&false;
&&&& 检查Bindings信息。
VerifyWebSiteBindingsIsExist
public&static&bool VerifyWebSiteBindingsIsExist(string bindingInfo)
&&& string temp =&string.E
&&& using (ServerManager mgr =&new ServerManager())
&&&&&&& for (int i =&<span style="color: #; i & mgr.Sites.C i++)
&&&&&&&&&&& foreach (Microsoft.Web.Administration.Binding b in mgr.Sites[i].Bindings)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& temp = b.BindingI
&&&&&&&&&&&&&&& if (temp.IndexOf('*') &&<span style="color: #)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& temp =&"*"&+
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& if (temp.Equals(bindingInfo))
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& return&true;
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&& return&false;
&&&& 以上代码均在Windows Vista SP1和Windows Server 2008上测试通过,使用时需要在工程中引用Microsoft.Web.Administration类库,该类库为IIS 7.0自带的。
阅读(...) 评论()下次自动登录
现在的位置:
& 综合 & 正文
如何用C#操纵IIS
using System.DirectoryS
using System.C
using System.Text.RegularE
using System.T
namespace Wuhy.ToolBox
这个类是静态类。用来实现管理IIS的基本操作。
管理IIS有两种方式,一是ADSI,一是WMI。由于系统限制的原因,只好选择使用ADSI实现功能。
这是一个遗憾。只有等到只有使用IIS 6的时候,才有可能使用WMI来管理系统
不过有一个问题就是,我现在也觉得这样的一个方法在本地执行会比较的好。最好不要远程执行。
因为那样需要占用相当数量的带宽,即使要远程执行,也是推荐在同一个网段里面执行
public class IISAdminLib
UserName,Password,HostName的定义#region UserName,Password,HostName的定义
public static string HostName
return hostN
hostName =
public static string UserName
return userN
userName =
public static string Password
if(UserName.Length &= <span style="COLOR: #)
throw new ArgumentException("还没有指定好用户名。请先指定用户名");
password =
public static void RemoteConfig(string hostName, string userName, string password)
HostName = hostN
UserName = userN
Password =
private static string hostName = "localhost";
private static string userN
private static string
#endregion
根据路径构造Entry的方法#region 根据路径构造Entry的方法
根据是否有用户名来判断是否是远程服务器。
然后再构造出不同的DirectoryEntry出来
/// DirectoryEntry的路径
/// 返回的是DirectoryEntry实例
public static DirectoryEntry GetDirectoryEntry(string entPath)
DirectoryE
if(UserName == null)
ent = new DirectoryEntry(entPath);
ent = new DirectoryEntry(entPath, HostName+"\\"+UserName, Password, AuthenticationTypes.Secure);
ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);
#endregion
添加,删除网站的方法#region 添加,删除网站的方法
创建一个新的网站。根据传过来的信息进行配置
/// 存储的是新网站的信息
public static void CreateNewWebSite(NewWebSiteInfo siteInfo)
if(! EnsureNewSiteEnavaible(siteInfo.BindString))
throw new DuplicatedWebSiteException("已经有了这样的网站了。" + Environment.NewLine + siteInfo.BindString);
string entPath = String.Format("IIS://{0}/w3svc", HostName);
DirectoryEntry rootEntry = GetDirectoryEntry(entPath);
string newSiteNum = GetNewWebSiteID();
DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer");
mitChanges();
newSiteEntry.Properties["ServerBindings"].Value = siteInfo.BindS
newSiteEntry.Properties["ServerComment"].Value = mentOfWebS
mitChanges();
DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
mitChanges();
vdEntry.Properties["Path"].Value = siteInfo.WebP
mitChanges();
删除一个网站。根据网站名称删除。
/// 网站名称
public static void DeleteWebSiteByName(string siteName)
string siteNum = GetWebSiteNum(siteName);
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
string rootPath = String.Format("IIS://{0}/w3svc", HostName);
DirectoryEntry rootEntry = GetDirectoryEntry(rootPath);
rootEntry.Children.Remove(siteEntry);
mitChanges();
#endregion
Start和Stop网站的方法#region Start和Stop网站的方法
public static void StartWebSite(string siteName)
string siteNum = GetWebSiteNum(siteName);
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
siteEntry.Invoke("Start", new object[] {});
public static void StopWebSite(string siteName)
<span id="Codehighlighter1__O
&&&&推荐文章:
【上篇】【下篇】

我要回帖

更多关于 iis应用程序池回收 的文章

 

随机推荐