C# 調用VS自帶程序WebDev.WebServer40.EXE 源代碼


通過Process.Start啟動,VS自帶程序WebDev.WebServer40.EXE

在內網架設網站時,為安裝IIS條件下用VS自帶的小程序來測試效果非常不錯!

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebServer
{
    public partial class FormApp : Form
    {
        public FormApp()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string _AppRoot = this.txtAppRoot.Text;
            string _Port = this.txtPort.Text;
            string _WebPath = this.txtWebPath.Text;
            string _WebvPath = this.txtWebvPath.Text;
            try
            {
                //簡單校驗
                CheckBoolTxt(_AppRoot, _Port, _WebPath, _WebvPath); 
                string arguments = string.Format("/port:{0} /path:{1} /vpath:{2}", _Port, _WebPath, _WebvPath);
                System.Diagnostics.Process.Start(_AppRoot, arguments);
                this.linkLabel1.Text = string.Format("http://localhost:{0}{1}", _Port, _WebvPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示:");
            }
        }

        private void CheckBoolTxt(string _AppRoot, string _Port, string _WebPath, string _WebvPath)
        {
            if (!System.IO.File.Exists(_AppRoot))
            {
                throw new Exception("找不到指定程序!");
            }
            if (!System.Text.RegularExpressions.Regex.IsMatch(_Port, @"^(([1-6][0-5][0-5][0-3][0-5])|([1-9][0-9][0-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9])|([1-9]))$"))  //^[1-6]?[\d]{0,4}$
            {
                throw new Exception("端口:1-65535之間未使用的端口號!");
            }
            if (!System.IO.Directory.Exists(_WebPath))
            {
                throw new Exception("物理路徑不存在,請指定有效的目錄!");
            }
            if (!_WebvPath.Contains("/"))
            {
                throw new Exception("虛擬目錄必須以'/'開頭!");
            }
            IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
            int counts = ipEndPoints.Count<IPEndPoint>(o => o.Port == int.Parse(_Port));
            if (counts > 0)
            {
                throw new Exception("端口已被占用!");
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(this.linkLabel1.Text);
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM