Web Server 启动工具
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.VisualBasic; using System.Runtime.Serialization.Json; using System.IO; namespace WebDevTool { public partial class frmMain : Form { private const string AppName = "WebDevTool"; private const string SectionName = "WebServers"; private const string ConfigSectionName = "Config"; private const string WebDevPathKey = "WebServerPath"; public frmMain() { InitializeComponent(); } public void RefreshUI() { if (!string.IsNullOrEmpty(GetWebDevServerPath())) txtExePath.Text = GetWebDevServerPath(); lstServers.Items.Clear(); var servers = GetAllWebServers(); foreach (var server in servers) { lstServers.Items.Add(server.Name); } lstServers.Refresh(); ClearInput(); lstServers.SelectedIndex = -1; } public void ClearInput() { txtServerName.Clear(); txtServerPort.Text = "80"; txtVPath.Clear(); txtServerDirectory.Clear(); } public void SaveWebDevServerPath(string path) { Interaction.SaveSetting(AppName, ConfigSectionName, WebDevPathKey, path); } public string GetWebDevServerPath() { return Interaction.GetSetting(AppName, ConfigSectionName, WebDevPathKey); } public void SaveSetting(string key, string value) { Interaction.SaveSetting(AppName, SectionName, key, value); } public string GetSetting(string key) { return Interaction.GetSetting(AppName, SectionName, key); } public void DeleteSetting(string key) { Interaction.DeleteSetting(AppName, SectionName, key); } public List<WebServer> GetAllWebServers() { var keys = new List<WebServer>(); var settings = Interaction.GetAllSettings(AppName, SectionName); if (settings != null) { for (var i = 0; i <= settings.GetUpperBound(0); i++) { try { var server = JsonHelper.JsonDeserialize<WebServer>(settings[i, 1]); keys.Add(server); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } return keys; } private void btnSelectExe_Click(object sender, EventArgs e) { if (System.IO.File.Exists(txtExePath.Text)) { openFileDialog1.FileName = txtExePath.Text; } if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { txtExePath.Text = openFileDialog1.FileName; SaveWebDevServerPath(openFileDialog1.FileName); } } private void btnPathSelect_Click(object sender, EventArgs e) { if (System.IO.Directory.Exists(txtServerDirectory.Text)) { folderBrowserDialog1.SelectedPath = txtServerDirectory.Text; } if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { txtServerDirectory.Text = folderBrowserDialog1.SelectedPath; } } private void frmMain_Load(object sender, EventArgs e) { RefreshUI(); } private void btnDelete_Click(object sender, EventArgs e) { DeleteSetting(lstServers.Text); RefreshUI(); } private void lstServers_SelectedIndexChanged(object sender, EventArgs e) { if (lstServers.SelectedIndex <0) { ClearInput(); btnDelete.Enabled = false; btnStart.Enabled = false; return; } btnStart.Enabled = true; btnDelete.Enabled = true; ClearInput(); try { var server = JsonHelper.JsonDeserialize<WebServer>(GetSetting(lstServers.Text)); txtServerName.Text = server.Name; txtServerPort.Text = server.Port; txtVPath.Text = server.HostPath; txtServerDirectory.Text = server.Directory; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtServerName.Text)) return; if (string.IsNullOrWhiteSpace(txtServerPort.Text)) return; if (string.IsNullOrWhiteSpace(txtServerDirectory.Text)) return; try { int.Parse(txtServerPort.Text); } catch (Exception) { return; } if (!System.IO.Directory.Exists(txtServerDirectory.Text)) return; var webServer = new WebServer(); webServer.Name = txtServerName.Text; webServer.Port = txtServerPort.Text; webServer.HostPath = txtVPath.Text; webServer.Directory = txtServerDirectory.Text; SaveSetting(webServer.Name, JsonHelper.JsonSerialize<WebServer>(webServer)); RefreshUI(); MessageBox.Show("Save Successfully!"); } private void btnNew_Click(object sender, EventArgs e) { lstServers.SelectedIndex = -1; } private void btnStart_Click(object sender, EventArgs e) { if(lstServers.SelectedIndex <0) return ; var server = JsonHelper.JsonDeserialize<WebServer>(GetSetting(lstServers.Text)); if (!System.IO.Directory.Exists(server.Directory)) { MessageBox.Show("Web Server Directory does not exist!"); return; } var cmd = string.Format("\"{0}\" /port:{1} /path:\"{2}\" /vpath:\"{3}\"", GetWebDevServerPath(), server.Port, server.Directory, server.HostPath); try { Interaction.Shell(cmd); } catch (Exception) { //MessageBox.Show("Cannot start the server on port {0}, maybe the port {0} has been used.", server.Port); } } } public class WebServer { public string Name { get; set; } public string Port { get; set; } public string HostPath { get; set; } public string Directory { get; set; } } public class JsonHelper { public static string JsonSerialize<T>(T t) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(); ser.WriteObject(ms, t); string jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } public static T JsonDeserialize<T>(string jsonString) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); T obj = (T)ser.ReadObject(ms); return obj; } } }
桂棹兮兰桨,击空明兮溯流光。