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);
        }
    }
}

原文地址:https://www.cnblogs.com/han1982/p/4093969.html