91网首页-91网页版-91网在线观看-91网站免费观看-91网站永久视频-91网站在线播放

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#操作IIS程序池及站點的創(chuàng)建配置實現(xiàn)代碼

admin
2018年9月8日 15:39 本文熱度 6040
最近在做一個WEB程序的安裝包,對一些操作IIS進行一個簡單的總結(jié),主要包括對IIS進行站點的新建以及新建站點的NET版本的選擇,還有針對IIS7程序池的托管模式以及版本的操作。

首先要對Microsoft.Web.Administration進行引用,它主要是用來操作IIS7:

using System.DirectoryServices;
using Microsoft.Web.Administration;


1:首先是對本版IIS的版本進行配置:

代碼如下:
DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
string Version = getEntity.Properties["MajorIISVersionNumber"].Value.ToString();
MessageBox.Show("IIS版本為:" + Version);

2:是判斷程序池是存在;

代碼如下:
/// <summary>
/// 判斷程序池是否存在
/// </summary>
/// <param name="AppPoolName">程序池名稱</param>
/// <returns>true存在 false不存在</returns>
private bool IsAppPoolName(string AppPoolName)
{
   bool result = false;
   DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
   foreach (DirectoryEntry getdir in appPools.Children)
   {
       if (getdir.Name.Equals(AppPoolName))
       {
           result = true;
       }
   }
   return result;
}

3:刪除應(yīng)用程序池

代碼如下:
/// <summary>
/// 刪除指定程序池
/// </summary>
/// <param name="AppPoolName">程序池名稱</param>
/// <returns>true刪除成功 false刪除失敗</returns>
private bool DeleteAppPool(string AppPoolName)
{
   bool result = false;
   DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
   foreach (DirectoryEntry getdir in appPools.Children)
   {
       if (getdir.Name.Equals(AppPoolName))
       {
           try
           {
               getdir.DeleteTree();
               result = true;
           }
           catch
          {
               result = false;
           }
       }
   }
   return result;
}

4:創(chuàng)建應(yīng)用程序池 (對程序池的設(shè)置主要是針對IIS7;IIS7應(yīng)用程序池托管模式主要包括集成跟經(jīng)典模式,并進行NET版本的設(shè)置)

代碼如下:
string AppPoolName = "LamAppPool";
if (!IsAppPoolName(AppPoolName))
{
   DirectoryEntry newpool;
   DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
   newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");
   newpool.CommitChanges();
   MessageBox.Show(AppPoolName + "程序池增加成功");
}
#endregion
#region 修改應(yīng)用程序的配置(包含托管模式及其NET運行版本)
ServerManager sm = new ServerManager();
sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";
sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated為集成 Classic為經(jīng)典
sm.CommitChanges();
MessageBox.Show(AppPoolName + "程序池托管管道模式:" + sm.ApplicationPools[AppPoolName].ManagedPipelineMode.ToString() + "運行的NET版本為:" + sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion);

運用C#代碼來對IIS7程序池托管管道模式及版本進行修改。

5:針對IIS6的NET版進行設(shè)置,因為此處我是用到NET4.0所以V4.0.30319,若是NET2.0則在這進行修改 v2.0.50727

代碼如下:
//啟動aspnet_regiis.exe程序
string fileName = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe";
ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
//處理目錄路徑
string path = vdEntry.Path.ToUpper();
int index = path.IndexOf("W3SVC");
path = path.Remove(0, index);
//啟動ASPnet_iis.exe程序,刷新腳本映射
startInfo.Arguments = "-s " + path;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
string errors = process.StandardError.ReadToEnd();

6:平常我們可能還得對IIS中的MIME類型進行增加,下面主要是我們用到兩個類型分別是:xaml,xap

代碼如下:
IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass();
NewMime.Extension = ".xaml"; NewMime.MimeType = "application/xaml+xml";
IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass();
TwoMime.Extension = ".xap"; TwoMime.MimeType = "application/x-silverlight-app";
rootEntry.Properties["MimeMap"].Add(NewMime);
rootEntry.Properties["MimeMap"].Add(TwoMime);
rootEntry.CommitChanges();

7:下面是做安裝時一段對IIS進行操作的代碼,兼容IIS6及IIS7,新建虛擬目錄并對相應(yīng)的屬性進行設(shè)置,對IIS7還進行新建程序池的程序,并設(shè)置程序池的配置

代碼如下:
/// <summary>
/// 創(chuàng)建網(wǎng)站
/// </summary>
/// <param name="siteInfo"></param>
public  void CreateNewWebSite(NewWebSiteInfo siteInfo)
{
   if (!EnsureNewSiteEnavaible(siteInfo.BindString))
   {
       throw new Exception("該網(wǎng)站已存在" + Environment.NewLine + siteInfo.BindString);
   }
   DirectoryEntry rootEntry = GetDirectoryEntry(entPath);
   newSiteNum = GetNewWebSiteID();
   DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer");
   newSiteEntry.CommitChanges();
   newSiteEntry.Properties["ServerBindings"].Value = siteInfo.BindString;
   newSiteEntry.Properties["ServerComment"].Value = siteInfo.CommentOfWebSite;
   newSiteEntry.CommitChanges();
   DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
   vdEntry.CommitChanges();
   string ChangWebPath = siteInfo.WebPath.Trim().Remove(siteInfo.WebPath.Trim().LastIndexOf(''\\''),1);
   vdEntry.Properties["Path"].Value = ChangWebPath;
   vdEntry.Invoke("AppCreate", true);//創(chuàng)建應(yīng)用程序
   vdEntry.Properties["AccessRead"][0] = true; //設(shè)置讀取權(quán)限
   vdEntry.Properties["AccessWrite"][0] = true;
   vdEntry.Properties["AccessScript"][0] = true;//執(zhí)行權(quán)限
   vdEntry.Properties["AccessExecute"][0] = false;
   vdEntry.Properties["DefaultDoc"][0] = "Login.aspx";//設(shè)置默認(rèn)文檔
   vdEntry.Properties["AppFriendlyName"][0] = "LabManager"; //應(yīng)用程序名稱          
   vdEntry.Properties["AuthFlags"][0] = 1;//0表示不允許匿名訪問,1表示就可以3為基本身份驗證,7為windows繼承身份驗證
   vdEntry.CommitChanges();

   //操作增加MIME
   //IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass();
   //NewMime.Extension = ".xaml"; NewMime.MimeType = "application/xaml+xml";
   //IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass();
   //TwoMime.Extension = ".xap"; TwoMime.MimeType = "application/x-silverlight-app";
   //rootEntry.Properties["MimeMap"].Add(NewMime);
   //rootEntry.Properties["MimeMap"].Add(TwoMime);
   //rootEntry.CommitChanges();

   #region 針對IIS7
   DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
   int Version =int.Parse(getEntity.Properties["MajorIISVersionNumber"].Value.ToString());
   if (Version > 6)
   {
       #region 創(chuàng)建應(yīng)用程序池
      string AppPoolName = "LabManager";
       if (!IsAppPoolName(AppPoolName))
       {
           DirectoryEntry newpool;
           DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
           newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");
           newpool.CommitChanges();
       }
       #endregion

      #region 修改應(yīng)用程序的配置(包含托管模式及其NET運行版本)
      ServerManager sm = new ServerManager();
       sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";
       sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated為集成 Classic為經(jīng)典
      sm.CommitChanges();
       #endregion

      vdEntry.Properties["AppPoolId"].Value = AppPoolName;
       vdEntry.CommitChanges();
   }
   #endregion


   //啟動aspnet_regiis.exe程序 
   string fileName = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe";
   ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
   //處理目錄路徑 
   string path = vdEntry.Path.ToUpper();
   int index = path.IndexOf("W3SVC");
   path = path.Remove(0, index);
   //啟動ASPnet_iis.exe程序,刷新腳本映射 
   startInfo.Arguments = "-s " + path;
   startInfo.WindowStyle = ProcessWindowStyle.Hidden;
   startInfo.UseShellExecute = false;
   startInfo.CreateNoWindow = true;
   startInfo.RedirectStandardOutput = true;
   startInfo.RedirectStandardError = true;
   Process process = new Process();
   process.StartInfo = startInfo;
   process.Start();
   process.WaitForExit();
   string errors = process.StandardError.ReadToEnd();
   if (errors != string.Empty)
   {
       throw new Exception(errors);
   }

}

代碼如下:

string entPath = String.Format("IIS://{0}/w3svc", "localhost");


public  DirectoryEntry GetDirectoryEntry(string entPath)
{
  DirectoryEntry ent = new DirectoryEntry(entPath);
  return ent;
}

public class NewWebSiteInfo
{
   private string hostIP;   // 主機IP
   private string portNum;   // 網(wǎng)站端口號
   private string descOfWebSite; // 網(wǎng)站表示。一般為網(wǎng)站的網(wǎng)站名。例如"www.dns.com.cn"
   private string commentOfWebSite;// 網(wǎng)站注釋。一般也為網(wǎng)站的網(wǎng)站名。
  private string webPath;   // 網(wǎng)站的主目錄。例如"e:\ mp"

  public NewWebSiteInfo(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath)
   {
       this.hostIP = hostIP;
       this.portNum = portNum;
       this.descOfWebSite = descOfWebSite;
       this.commentOfWebSite = commentOfWebSite;
       this.webPath = webPath;
   }

  public string BindString
  {
       get
       {
           return String.Format("{0}:{1}:{2}", hostIP, portNum, descOfWebSite); //網(wǎng)站標(biāo)識(IP,端口,主機頭值)
      }
   }

  public string PortNum
   {
       get
       {
           return portNum;
       }
   }

  public string CommentOfWebSite
   {
       get
       {
           return commentOfWebSite;
       }
   }

  public string WebPath
   {
      get
       {
           return webPath;
      }
   }
}


8:下面的代碼是對文件夾權(quán)限進行設(shè)置,下面代碼是創(chuàng)建Everyone 并給予全部權(quán)限

代碼如下:
/// <summary>
/// 設(shè)置文件夾權(quán)限 處理給EVERONE賦予所有權(quán)限
/// </summary>
 /// <param name="FileAdd">文件夾路徑</param>
 public void SetFileRole()
 {
     string FileAdd = this.Context.Parameters["installdir"].ToString();
     FileAdd = FileAdd.Remove(FileAdd.LastIndexOf(''\\''), 1);
     DirectorySecurity fSec = new DirectorySecurity();
     fSec.AddAccessRule(new FileSystemAccessRule("Everyone",FileSystemRights.FullControl,InheritanceFlags.ContainerInherit|InheritanceFlags.ObjectInherit,PropagationFlags.None,AccessControlType.Allow));
     System.IO.Directory.SetAccessControl(FileAdd, fSec);
 }

該文章在 2018/9/8 15:39:41 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 中文字幕在线第一页 | 午夜影视在线播 | 精品一区二区ww | 欧美在线视频第一页 | 日韩欧美在线综合网 | 国产精品在线二三区 | 日本漫遊網站 | 日韩在线观看视频 | 午夜国产短视频 | 欧美在线观看网址 | 午夜小福利 | 成人免费黄 | 人人色在线视频播放 | 日本阿v视频免 | 国产精品视频 | 国产精品最新网址 | 国产午夜福利精品 | 国产精品区免费视频 | 成人午夜免费视频 | 韩国午夜理伦 | 欧美日韩午 | 国语自产拍 | 91色在线观看 | 成人国产精品高清 | 区二区三区 | 最新精品国偷自产在线91 | 国产精品专区第5页 | 日本无卡码| 国产欧美日韩亚洲区 | 日韩精品在线播放 | 欧美三级短视频 | 九九热99最新地址 | 国产精品码 | 国产一区在线精品 | 精品一线二线在线 | 国产一区在线精品 | 91影视网 | 精品国产免费1区 | 欧美日韩成人精品 | 韩国一区二区三区 | 国产剧视频在线播放 |