我最近尝试执行以下行;
string strCmdText;
strCmdText = "netstat -np TCP | find " + quote + number + quote + "";
System.Diagnostics.Process.Start("netstat.exe", strCmdText);
Logs.Write("LISTEN_TO(" + Registry_val1.Text + ")", strCmdText);
现在要做的就是基本找到所有包含'80'的TCP端口并在我自定义的日志系统中显示它们,这将在我的文件夹中创建一个日志;
LISTEN_TO(80) - {DATE_TIME} .TXT 在这个.txt里面它应该包含命令发出的文本,但是我得到的只是时间。
我如上所述调试了这个命令,不幸的是我所知道的是CMDtext设置正确,我的日志记录系统正常工作,让我无法选择一旦查询启动就关闭NETSTAT? / p>
希望我提供了足够的信息,因为这是我的第一篇文章。
此致
钴
由于含糊不清的描述,这里是我尝试过的另一种相同的代码,但仍然只有一段时间。
const string quote = "\"";
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "netstat -np TCP | find " + quote + number + quote + "";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
String output = p.StandardOutput.ReadToEnd();
Logs.Write("LISTEN_TO(" + Registry_val1.Text + ")", output);
基本上,你可以看到这个; textbox1.text = output; execpt现在输出被放到日志文件中。
答案 0 :(得分:3)
我不明白为什么你首先使用netstat。 .Net框架有一大堆类,可以提供所有类型的数据,在这种情况下,IPGlobalProperties具有您需要的方法。
var ip = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
foreach(var tcp in ip.GetActiveTcpConnections())
{
if (tcp.LocalEndPoint.Port == number
|| tcp.RemoteEndPoint.Port == number)
{
Logs.Write(
String.Format(
"{0} : {1}",
tcp.LocalEndPoint.Address,
tcp.RemoteEndPoint.Address));
}
}
使用内置类的好处是可以轻松地整形和选择您需要的任何内容,最重要的是:您为自己和用户节省了进程外调用和解析输出。
答案 1 :(得分:1)
你可以试试这个:
strCmdText = "cmd /c \"netstat -np TCP | find " + quote + number + quote + "\"";
如果这不起作用,请首先在cmd提示符中使用该命令以确保它返回数据。
cmd /c "netstat -an | find "80"