我是一个新开发人员,所以我有很多问题:
在这个类中,我获得磁盘上的文件路径(需要转换为pcap扩展名的wireshark文件),并使用方法convertFileToPcap()、调用convertFileToPcap()的构造函数和创建Editcap对象后的主函数将其转换为文件路径(pcap ),属性getNewFileName()返回_newFileName (类成员),我想知道是否有更好的方法或适当的方法来实现它。
public class Editcap
{
#region members
private string _editpcap;
private ProcessStartInfo _editpcapProcess;
private FileInfo _fileInfo;
private string _newFileName;
#endregion
#region c'tor
public Editcap(FileInfo fileinfo)
{
_fileInfo = fileinfo;
_newFileName = "";
convertFileToPcap();
}
#endregion
public void convertFileToPcap()
{
string oldFileExtension = _fileInfo.Extension;
_newFileName = _fileInfo.FullName.Replace(oldFileExtension, "_new") + ".pcap";
_editpcapProcess = new ProcessStartInfo(string.Format("\"{0}\"", _editpcap));
_editpcapProcess.Arguments = (string.Format("{2}{0}{2} -F libpcap {2}{1}{2}", _fileInfo.FullName, _newFileName, "\""));
_editpcapProcess.WindowStyle = ProcessWindowStyle.Hidden;
_editpcapProcess.RedirectStandardOutput = true;
_editpcapProcess.RedirectStandardError = true;
_editpcapProcess.CreateNoWindow = true;
_editpcapProcess.UseShellExecute = false;
_editpcapProcess.ErrorDialog = false;
Process capinfosProcess = Process.Start(_editpcapProcess);
capinfosProcess.WaitForExit();
}
public string getNewFileName()
{
return _newFileName;
}
}发布于 2012-12-07 15:07:32
你好,欢迎来到代码评论!
public!_fileInfo和_newFileName。让其他人保持本土化的方法。_editpcap从来不被分配,并且始终是null。我不认为这是你想要的。getNewFileName方法看起来非常像Java,因为Java没有属性的概念。这是被授予财产的主要候选人。Process类是IDisposable,因此将它封装在using块中是惯用的。readonly关键字来说明它的意图。此外,您还可以使用现代的C#设施作为对象初始化器和var关键字。尽管如此,以下是我提议的代码重构:
public class Editcap
{
#region members
private readonly FileInfo fileInfo;
private string newFileName = string.Empty;
#endregion
#region c'tor
public Editcap(FileInfo fileinfo)
{
if (fileinfo == null)
{
throw new ArgumentNullException("fileInfo");
}
this.fileInfo = fileinfo;
}
#endregion
public void ConvertFileToPcap()
{
this.newFileName = this.fileInfo.FullName.Replace(this.fileInfo.Extension, "_new") + ".pcap";
string editpcap = null; // still not set, need to fix!
var editpcapProcess = new ProcessStartInfo(string.Format("\"{0}\"", editpcap))
{
Arguments = string.Format("{2}{0}{2} -F libpcap {2}{1}{2}", this.fileInfo.FullName, this.newFileName, "\""),
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false,
ErrorDialog = false
};
using (var capinfosProcess = Process.Start(editpcapProcess))
{
capinfosProcess.WaitForExit();
}
}
public string NewFileName
{
get
{
return this.newFileName;
}
}
}https://codereview.stackexchange.com/questions/19385
复制相似问题