代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
查看本机开放的端口
作者:
Dezai.CN
/ 发布于
2011/6/6
/
524
本机开放的端口
<div> <div><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Diagnostics; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Text.RegularExpressions; </span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> ShowPort_Click(</span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> sender, EventArgs e) { StringBuilder str1 </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> StringBuilder();</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">用于存储截获DOS控制台的输出流</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> []str2</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(128,0,128)">2000</span><span style="color: rgb(0,0,0)">];</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">存储端口号</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> Process p </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Process(); p.StartInfo.FileName </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">cmd.exe</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; p.StartInfo.RedirectStandardInput </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; p.StartInfo.RedirectStandardOutput </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; p.StartInfo.RedirectStandardError </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; p.StartInfo.CreateNoWindow </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; p.StartInfo.UseShellExecute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; p.Start(); p.StandardInput.WriteLine(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">netstat -an</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); p.StandardInput.WriteLine(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">exit</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,0)"> str1.AppendLine(p.StandardOutput.ReadToEnd()) ; p.Close(); </span></div> <div><span style="color: rgb(0,0,0)"> Regex rx </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Regex(</span><span style="color: rgb(128,0,0)">@"</span><span style="color: rgb(128,0,0)">(:{1}\d+)</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">用正则表达式筛选端口号,即":"号后面全是</span></div> <div><span style="color: rgb(0,0,0)"> MatchCollection mc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> rx.Matches(str1.ToString());</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">得到一个前缀带":"端口号的的数组</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> Pcount </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">记录有效端口号个数</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> mc.Count; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> temp </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">""</span><span style="color: rgb(0,0,0)">; temp </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> mc[i].ToString(); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (check(str2, temp.Remove(</span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">)))</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">判断是否已存在的端口</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { str2[Pcount</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">] </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> temp.Remove(</span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">去掉前缀":"</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } } </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> Pcount; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { comboBox1.Items.Add(str2[i]);</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">用一个comBox加载显示</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> check(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] source, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> checker)</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">检查是否有相同端口号</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> source.Length; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (checker </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> source[i]) </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; }</span></div> </div>
试试其它关键字
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3