我目前正在编写一个C#程序,它具有类似于wireshack的功能,使用SharpPcap来捕获数据包,使用PacketDotNet来获取有关数据包的信息。我想知道如何获取与数据包关联的进程名称??
发布于 2013-03-10 21:56:03
您可以通过解析来自netstat -o的输出来获得ProcessId,然后从Process.GetById获得进程名。
这段代码可能会有帮助,但我不太擅长regexp :)
var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "netstat",
Arguments = "-on",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
Regex r = new Regex(@"\S+\s+(?<address>\S+)\s+\S+\s+\S+\s+(?<pid>\d+)");
while (!proc.StandardOutput.EndOfStream) {
var res = r.Match(proc.StandardOutput.ReadLine());
if (res.Success) {
var pid = int.Parse(res.Groups["pid"].Value);
var address = res.Groups["address"].Value;
Console.WriteLine("{0} - {1}", address, Process.GetProcessById(pid).ProcessName);
}
}https://stackoverflow.com/questions/15321571
复制相似问题