我正在构建打开 Wireshark 服务的应用程序(Wireshark 有几个服务),以便对 Wireshark 文件进行不同的操作,例如编辑、更改格式、统计信息等...每个选项通常使用不同的服务,所以我想用继承来构建我的类想知道我想做的是适当的方式还是适当的方式:
具有以下成员和方法的主类 WiresharkServices: 这是所有其他类都应继承的类
public class WiresharkProcesses
{
protected string _filePath; //the file path who send to Wiresahrk process
protected string _capinfos;
protected string _dumpcap;
protected string _editcap;
protected string _mergecap;
protected string _rawshark;
protected string _text2pcap;
protected string _tshark;
protected string _wireshark;
public void initializeServices()
{
if (Directory.Exists(@"C:\Program Files (x86)\Wireshark"))
{
_capinfos = @"C:\Program Files (x86)\Wireshark\_capinfos.exe";
_dumpcap = @"C:\Program Files (x86)\Wireshark\_dumpcap.exe";
_editcap = @"C:\Program Files (x86)\Wireshark\editcap.exe";
_mergecap = @"C:\Program Files (x86)\Wireshark\_mergecap.exe";
_rawshark = @"C:\Program Files (x86)\Wireshark\_rawshark.exe";
_text2pcap = @"C:\Program Files (x86)\Wireshark\_text2pcap.exe";
_tshark = @"C:\Program Files (x86)\Wireshark\_tshark.exe";
_wireshark = @"C:\Program Files (x86)\Wireshark\_wireshark.exe";
}
else if (Directory.Exists(@"C:\Program Files\Wireshark"))
{
_capinfos = @"C:\Program File)\Wireshark\_capinfos.exe";
_dumpcap = @"C:\Program Files\Wireshark\_dumpcap.exe";
_editcap = @"C:\Program Files\Wireshark\editcap.exe";
_mergecap = @"C:\Program Files\Wireshark\_mergecap.exe";
_rawshark = @"C:\Program Files\Wireshark\_rawshark.exe";
_text2pcap = @"C:\Program Files\Wireshark\_text2pcap.exe";
_tshark = @"C:\Program Files\Wireshark\_tshark.exe";
_wireshark = @"C:\Program Files\Wireshark\_wireshark.exe";
}
}
}
当应用程序运行时,我当然会检查机器上是否安装了 Wireshark,如果没有抛出异常并且在 Wireshark 中存在:
WiresharkServices wservices = new WiresharkServices();
wservices .initializeServices();
并且在每个类中都有自己的方法。
接收文件路径以将其转换为其他 Wireshark 格式的子类示例:
public class Editcap : WiresharkProcesses
{
private string _newFileName;
public void startProcess(string filePath)
{
FileInfo file = new FileInfo(filePath);
_newFileName = file.FullName.Replace(file.Extension, "_new") + ".pcap";
ProcessStartInfo editcapProcess = new ProcessStartInfo(string.Format("\"{0}\"", _editcap))
{
Arguments = string.Format("{2}{0}{2} -F libpcap {2}{1}{2}", file.FullName, _newFileName, "\""),
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false,
ErrorDialog = false
};
using (Process editcap = Process.Start(editcapProcess))
{
editcap.WaitForExit();
}
}
public string getNewFileName()
{
return _newFileName;
}
}