端口是否使用

//检查端口是否被占用!
public static bool PortInUseBl(int port)
{
bool inUse = false;

IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();//获取目前端口列表
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();//

foreach (IPEndPoint endPoint in ipEndPoints)
{
if (endPoint.Port == port)
{
inUse = true;
break;
}
}

return inUse;
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
using System.Net.NetworkInformation;
using System.Net;

namespace DCSoft.ASPNETDemoRunner
{
public partial class Frm_wb : Form
{
string exeip;
BsnameId bsnameId = new BsnameId();
String srdps40 = "10.0\WebDev.WebServer40.exe";
String srdps20 = "10.0\WebDev.WebServer20.exe";
Boolean bldps = false;
public Frm_wb()
{
InitializeComponent();

}
private void Btn_Open_Click(object sender, EventArgs e)
{

//判断端口有没有被使用
int port=9989;
Boolean blport;
Boolean blbp=true;
blport = PortInUse.PortInUseBl(port);
while (blport) {
port += 1;
blport = PortInUse.PortInUseBl(port);
if (port > 65535 && blbp)
{
port = 1;
blbp = false;
}
}
String id = port.ToString();
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowDialog();
if (fbd.SelectedPath != "")
{
//path 路径中有空格的时候,可以这么写 "/port:{0} "/path:"{1} /vpath:{2}",其中的"/path:"就是可以有空格
string arguments = string.Format("/port:{0} "/path:"{1} /vpath:{2}", id, fbd.SelectedPath, "/");//fbd.SelectedPath asp.net文件地址
if (bldps)
{
System.Diagnostics.Process.Start(srdps20, arguments);//存在+20,运行asp.net
wb.Url = new Uri("http://localhost:" + id + "/");
cmb_url.Text = Convert.ToString(wb.Url);
MessageBox.Show("端口:" + id);
}
else
{
System.Diagnostics.Process.Start(srdps40, arguments);//存在40,运行asp.net
wb.Url = new Uri("http://localhost:" + id + "/");
cmb_url.Text = Convert.ToString(wb.Url);
MessageBox.Show("端口:" + id);
}
}
else
{
MessageBox.Show("没有选择路径");
}
}
private void cmb_url_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (cmb_url.Text!="")
{
try
{
cmb_url.Items.Add(cmb_url.Text);
wb.Url = new Uri(cmb_url.Text);
//this.comboBox1.SelectedIndex = 0;
}
catch { }
}
}
//MessageBox.Show(e.KeyCode.ToString());

}
private void Frm_wb_Load(object sender, EventArgs e)
{

//if (!File.Exists(srdps40) && !File.Exists(srdps20))
//{
// Filecopy.CopyFolder(@"C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer", System.Windows.Forms.Application.StartupPath + @"10.0");
//}
//else if (!File.Exists(srdps40)) { bldps = true; }

//if (!File.Exists(@"C:WindowsassemblyGAC_32WebDev.WebHost4010.0.0.0__b03f5f7f11d50a3a\webdev.webhost40.dll"))
//{
// if (!Directory.Exists(@"C:WindowsassemblyGAC_32WebDev.WebHost4010.0.0.0__b03f5f7f11d50a3a"))
// {
// Directory.CreateDirectory(@"C:WindowsassemblyGAC_32WebDev.WebHost4010.0.0.0__b03f5f7f11d50a3a");
// }
// File.Copy(@"webdev.webhost40.dll", @"C:WindowsassemblyGAC_32WebDev.WebHost4010.0.0.0__b03f5f7f11d50a3a\webdev.webhost40.dll", true);
//}
wb.Url = new Uri("http://www.dcwriter.cn/");
this.KeyPreview = true;
cmb_url.Text = "http://www.dcwriter.cn/";
//webBrowser1.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);



//System.Diagnostics.Process.Start(WebDev.WebServer/ItemBoundsPortion:9989/Path:"c:inetpubwwwrootMyapp"/vpath:"/Myapp");
}
private void Frm_wb_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr =

MessageBox.Show(this, "确定退出?",

"退出窗口通知", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (dr != DialogResult.Yes)
{

//3.Cancel 取得或設定數值,表示是否應該取消事件。

e.Cancel = true;

}
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();

foreach (System.Diagnostics.Process myProcess in myProcesses)
{
try
{
if ("WebDev.WebServer40" == myProcess.ProcessName)
myProcess.Kill();//强制关闭该程序
if ("WebDev.WebServer20" == myProcess.ProcessName)
myProcess.Kill();//强制关闭该程序
}
catch { }
}
}
private void Btn_Google_Click(object sender, EventArgs e)
{
exeip = bsnameId.BsnmIdtoString("chrome");
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip, wb.Url.ToString());
}

}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!");}
}
private void Btn_Firefox_Click(object sender, EventArgs e)
{
exeip = bsnameId.BsnmIdtoString("Firefox");
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip, wb.Url.ToString());
}

}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!");}
}
private void Btn_UC_Click(object sender, EventArgs e)
{
exeip = bsnameId.BsnmIdtoString("UCBrowser");
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip, wb.Url.ToString());
}

}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!"); }
}
private void Btn_QQ_Click(object sender, EventArgs e)
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\Wow6432Node\Tencent\QQBrowser", false))
{
if (key != null)//判断对象存在
{
exeip = key.GetValue("Exe", "").ToString();//获取安装路径

}

}
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip, wb.Url.ToString());
}

}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!");}
}
private void Btn_Baidu_Click(object sender, EventArgs e)
{
exeip = bsnameId.BsnmIdtoString("baidubrowser");
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip, wb.Url.ToString());
}

}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!");}
}
private void Btn_IE_Click(object sender, EventArgs e)
{
exeip = bsnameId.BsnmIdtoString("iexplore");
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip, wb.Url.ToString());
}

}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!");}
}
private void Btn_360_Click(object sender, EventArgs e)
{
//exeip = Bsnmip2("360Chrome");
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("HKEY_USERS\S-1-5-21-2145859812-3201629969-2626182436-1001\SOFTWARE\360\360se6\Chrome", false))
{
if (key != null)//判断对象存在
{
exeip = key.GetValue("last_install_path", "").ToString();//获取安装路径
}
}
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip + "360se6\Application\360se.exe", wb.Url.ToString());
}
}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!"); }
}
private void Btn_Safari_Click(object sender, EventArgs e)
{
exeip = bsnameId.BsnmIdtoString("Safari");
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip, wb.Url.ToString());
}

}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!"); }
}
private void Btn_Opera_Click(object sender, EventArgs e)
{
exeip = bsnameId.BsnmIdttoString("Opera");
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip + "\launcher.exe", wb.Url.ToString());
}

}
catch
{
MessageBox.Show("注册表中没有此浏览器!请确认是否安装!");
}
}
private void Btn_Edge_Click(object sender, EventArgs e)
{
exeip = bsnameId.BsnmIdtoString("MicrosoftEdge");
try
{
if (exeip != "")
{
System.Diagnostics.Process.Start(exeip, wb.Url.ToString());
}

}
catch { MessageBox.Show("注册表中没有此浏览器!请确认是否安装!"); }
}
private void Btn_Reload_Click(object sender, EventArgs e)
{
wb.Url = new Uri(cmb_url.Text);
}



private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
cmb_url.Text = Convert.ToString(wb.Url);
}

private void cmb_url_SelectedIndexChanged(object sender, EventArgs e)
{
wb.Url = new Uri(cmb_url.Text);
}

private void btnurlrf_Click(object sender, EventArgs e)
{
wb.Url = new Uri(cmb_url.Text);
}

private void wb_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
progressBar1.Visible = true;

if ((e.CurrentProgress > 0) && (e.MaximumProgress > 0))
{
progressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);//设置正在加载的文档总字节数
progressBar1.Step = Convert.ToInt32(e.CurrentProgress);////获取已下载文档的字节数
progressBar1.PerformStep();
}

else if (wb.ReadyState == WebBrowserReadyState.Complete)//加载完成后隐藏进度条
{
progressBar1.Value = 0;
progressBar1.Visible = false;
}

}

private void btngoback_Click(object sender, EventArgs e)
{
wb.GoBack();
}
}
}

原文地址:https://www.cnblogs.com/1175429393wljblog/p/5292803.html