Whether the port is used

Source: Internet
Author: User

Check if the port is occupied!
public static bool Portinusebl (int port)
{
BOOL InUse = false;

Ipglobalproperties ipproperties = Ipglobalproperties.getipglobalproperties ();//Get Current port list
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)
{

Determine if the port is being used
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! = "")
{
You can write "/port:{0} \"/path:\ "{1}/vpath:{2}" when there are spaces in the path, where \ "/path:\" can have spaces
string arguments = String. Format ("/port:{0} \"/path:\ "{1}/vpath:{2}", ID, FBD. SelectedPath, "/");//fbd. SelectedPath ASP. Net file Address
if (Bldps)
{
System.Diagnostics.Process.Start (SRDPS20, arguments);//exist +20, run ASP.
Wb. URL = new Uri ("http://localhost:" + ID + "/");
Cmb_url. Text = convert.tostring (wb. URL);
MessageBox.Show ("Port:" + ID);
}
Else
{
System.Diagnostics.Process.Start (SRDPS40, arguments);//exist 40, run ASP.
Wb. URL = new Uri ("http://localhost:" + ID + "/");
Cmb_url. Text = convert.tostring (wb. URL);
MessageBox.Show ("Port:" + ID);
}
}
Else
{
MessageBox.Show ("No Path selected");
}
}
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:\Windows\assembly\GAC_32\WebDev.WebHost40\10.0.0.0__b03f5f7f11d50a3a\\webdev.webhost40.dll"))
//{
if (! Directory.Exists (@ "C:\Windows\assembly\GAC_32\WebDev.WebHost40\10.0.0.0__b03f5f7f11d50a3a"))
// {
Directory.CreateDirectory (@ "C:\Windows\assembly\GAC_32\WebDev.WebHost40\10.0.0.0__b03f5f7f11d50a3a");
// }
File.Copy (@ "Webdev.webhost40.dll", @ "C:\Windows\assembly\GAC_32\WebDev.WebHost40\10.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:\inetpub\wwwroot\Myapp"/ Vpath: "/myapp");
}
private void Frm_wb_formclosing (object sender, FormClosingEventArgs e)
{
DialogResult dr =

MessageBox.Show (This, "OK exit?") ",

"Exit Window Notification", Messageboxbuttons.yesno, messageboxicon.question);

if (dr! = Dialogresult.yes)
{

3.Cancel Gets or set the value indicating whether the event should be canceled.

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 ();//force the program to close
if ("webdev.webserver20" = = Myprocess.processname)
Myprocess.kill ();//force the program to close
}
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 ("This browser is not in the registry! Please make sure it is installed! ");}
}
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 ("This browser is not in the registry! Please make sure it is installed! ");}
}
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 ("This browser is not in the registry! Please make sure it is installed! "); }
}
private void Btn_qq_click (object sender, EventArgs e)
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey ("Software\\wow6432node\\tencent\\qqbrowser", false))
{
if (key! = NULL)//Judgment object exists
{
Exeip = key. GetValue ("Exe", ""). ToString ();//Get the installation path

}

}
Try
{
if (Exeip! = "")
{
System.Diagnostics.Process.Start (EXEIP, WB. Url.tostring ());
}

}
catch {MessageBox.Show ("This browser is not in the registry! Please make sure it is installed! ");}
}
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 ("This browser is not in the registry! Please make sure it is installed! ");}
}
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 ("This browser is not in the registry! Please make sure it is installed! ");}
}
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)//Judgment object exists
{
Exeip = key. GetValue ("Last_install_path", ""). ToString ();//Get the installation path
}
}
Try
{
if (Exeip! = "")
{
System.Diagnostics.Process.Start (Exeip + "360se6\\application\\360se.exe", WB. Url.tostring ());
}
}
catch {MessageBox.Show ("This browser is not in the registry! Please make sure it is installed! "); }
}
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 ("This browser is not in the registry! Please make sure it is installed! "); }
}
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 ("This browser is not in the registry! Please make sure it is installed! ");
}
}
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 ("This browser is not in the registry! Please make sure it is installed! "); }
}
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);//Set the total number of bytes in the document being loaded
Progressbar1.step = Convert.ToInt32 (e.currentprogress);////Gets the number of bytes of the downloaded document
Progressbar1.performstep ();
}

else if (WB. ReadyState = = webbrowserreadystate.complete)//Hide progress bar after loading complete
{
progressBar1.Value = 0;
Progressbar1.visible = false;
}

}

private void Btngoback_click (object sender, EventArgs e)
{
Wb. GoBack ();
}
}
}

Whether the port is used

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.