C# ASP.net執行APPcmd/DNScmd實踐網站自動平台化

最近在測試,如果透過web申請帳號後,可不可以直接在DNS上一增子網域以及在IIS上增加站台的方式,直接申請後就有站台,在Windows 的網站主機上,似乎要透過appcmd與dnscmd達到。後來弄出了一個可以執行的方式,主要重點在於要有較高權限的帳密透過process執行cmd模式再去下指令。分享一下

/// <summary>

        /// 新增IIS站台

        /// </summary>

        /// <param name=”subdomain”>網站名稱</param>

        /// <param name=”domain”>網域</param>

        /// <param name=”webpath”>網頁位置</param>

        /// <returns></returns>

        public bool AddSite(string subdomain, string domain, string webpath)

        {

            using (Process p = new Process())

            {

                p.StartInfo.FileName = @”cmd.exe”;

                p.StartInfo.UseShellExecute = false;

                p.StartInfo.CreateNoWindow = true;

                p.StartInfo.RedirectStandardError = true;

                p.StartInfo.RedirectStandardInput = true;

                p.StartInfo.RedirectStandardOutput = true;

                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                p.StartInfo.UserName = “高權限帳號”;

                string pw = “高權限密碼”;

                SecureString ss = new SecureString();   

                foreach (char c in pw)

                {

                    ss.AppendChar(c);

                }

                p.StartInfo.Password = ss;

                //

                p.StartInfo.Arguments = “/c appcmd add site /name:”+subdomain+” /bindings:\”http/*:80:”+subdomain+”.”+domain+”,https/*:443:”+subdomain+”.”+domain+”\” /physicalPath:\””+webpath+”\””;

                p.Start();

                p.WaitForExit();

                StreamReader sr = p.StandardOutput;

                p.Close();

                string message = sr.ReadToEnd().Replace(“\n”, “<br />”);

                if (message.Contains(“ERROR”))

                {

                    return false;

                }

                else

                {

                    return true;

                }

            }

        }

        /// <summary>

        /// 透過指令新增子網域(如: levin.ksi.com.tw)

        /// </summary>

        /// <param name=”subdomain”>子網域名稱</param>

        /// <param name=”domain”>主網域</param>

        /// <param name=”ip”>主機IP</param>

        /// <returns></returns>

        public bool AddDNS(string subdomain,string domain,string ip)

        {

            using (Process p = new Process())

            {

                p.StartInfo.FileName = @”cmd.exe”;

                p.StartInfo.UseShellExecute = false;

                p.StartInfo.CreateNoWindow = true;

                p.StartInfo.RedirectStandardError = true;

                p.StartInfo.RedirectStandardInput = true;

                p.StartInfo.RedirectStandardOutput = true;

                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                p.StartInfo.UserName = “高權限帳號”;

                string pw = “高權限密碼”;

                SecureString ss = new SecureString();

                foreach (char c in pw)

                {

                    ss.AppendChar(c);

                }

                p.StartInfo.Password = ss;

                p.StartInfo.Arguments = @”/c dnscmd /recordadd ” + domain + ” ” + subdomain + ” A ” + ip;

                p.Start();

                p.WaitForExit();

                StreamReader sr = p.StandardOutput;

                p.Close();

                string message = sr.ReadToEnd().Replace(“\n”, “<br />”);

                if (message.Contains(“成功”)){

                    return true;

                }

                else

                {

                    return false;

                }

            }

        }

CodeIgniter安裝

一直習慣使用純PHP寫程式,不過最近看到許多徵才網站都寫著會使用CodeIgniter框架更佳。於是就來學習使用看看,沒想到還真的簡單易用,分享給大家。

 

1. 到CodeIgniter官方網站下載(http://ellislab.com/codeigniter),或是目前台灣也有繁體中文網站(http://www.codeigniter.org.tw/)。

2.下載後解壓縮,只要上傳application、system二個目錄及index.php到主機即可。

3.確認上傳後,就可以看到Welcome to CodeIgniter!

CodeIgniter

4.恭禧您!已經踏入CodeIgniter了XDDD