简体   繁体   English

哪种方法更好?

[英]Which method is better?

I am using an import to open a connected physical hard-drive: 我正在使用导入打开连接的物理硬盘驱动器:

var sfh = Imports.CreateFile(Path, Imports.FileAccess.GenericAll, Imports.FileShare.None, IntPtr.Zero, Imports.CreationDisposition.OpenExisting, 0, IntPtr.Zero);
if (sfh.IsInvalid)
{
    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
    return;
}
Geometry = Imports.GetGeometry(sfh);
var fs = new FileStream(sfh, FileAccess.ReadWrite, (int)Geometry.BytesPerSector, false);

That works fine, but instead of using FileStream, I was wondering if this would be a more efficient way to read bytes from the drive: http://msdn.microsoft.com/en-us/library/aa365467%28v=VS.85%29.aspx 效果很好,但是我想知道这是否是从驱动器读取字节的更有效方法,而不是使用FileStream: http : //msdn.microsoft.com/zh-cn/library/aa365467%28v=VS。 85%29.aspx

Is speed and/or efficiency that important for you ?? 速度和/或效率对您来说重要吗? because the difference is probably minor in this case... 因为在这种情况下差异可能很小。

It seems like the link you gave uses a WinAPI method. 您提供的链接似乎使用了WinAPI方法。 I would avoid using these were you don't necessarily have to since the .net GarbageCollector doesn't play well with Native resources, and you might suffer from memory leaks if you don't handle these correctly... 我会避免使用这些,因为您不必一定要使用它们,因为.net GarbageCollector不能与本机资源很好地配合使用,如果处理不当,您可能会遭受内存泄漏的困扰...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM