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

  • 相关阅读:
    appium知识01-环境设置
    移动端测试基础知识02
    魔术方法和反射
    面向对象开发: 封装, 继承, 多态
    正则的用法
    内置方法, 第三方模块(math, random, pickle, json, time, os, shutil, zip, tarfile), 导入包
    推导式(列表, 集合, 字典), 生成器
    迭代器, 高阶函数(map, filter, reduce, sorted) , 递归函数
    函数globals和locals用法, LEGB原则, 闭包函数 , 匿名函数
    字符串, 列表, 元祖, 集合, 字典的相关操作和函数, 深浅copy
  • 原文地址:https://www.cnblogs.com/han1982/p/4093969.html
Copyright © 2020-2023  润新知