使用API​​代码的磁盘读/写字节 [英] Disk Read/Write Bytes using API code

查看:63
本文介绍了使用API​​代码的磁盘读/写字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取每个磁盘(或每个分区)的使用情况报告,即读取/写入字节

i知道我可以使用性能计数器,甚至WMI,但我想通过使用原生WinAPI和p / invoke实现相同的结果



有没有人可以指向正确的函数或库来查询这些细节?

i假设我需要一个函数来返回当前分区,然后返回读取/写入总数或其他类似的东西?



这个是用的在vb.net代码中,但是猜测这在现阶段并不重要。



问候,

解决方案

< blockquote>

creizlein写道:

你是对的,我知道它是一条不应该采取的肮脏之路,使用托管库是很多的更好,但我需要避免它们出于某些其他特定原因,这就是我要求api功能的原因,是的,那些将从VB.net调用,这就是为什么我添加标签,但也许我应该删除它。如果不合适,我道歉..

正如我所说,你可以自己找到合适的原始Windows API。



请参阅:

http://msdn.microsoft.com/en-我们/ library / windows / desktop / aa365465%28v = vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364417%28v= vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363798%28v=vs.85%29.aspx [ ^ ]。



参见: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364407%28v=vs.85%29.aspx [ ^ ]。br />


-SA


当然。您将需要使用低级磁盘访问,以下原始Windows API:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939%28v=vs.85%29.aspx [< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939%28v=vs.85%29.aspxtarget =_ blanktitle =New Window> ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en -us / library / windows / desktop / aa365541%28v = vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85% 29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365468%28v=vs.85%29.aspx [ ^ ],

http: //msdn.microsoft.com/en-us/library/windows/desktop/aa365747%28v=vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365748%28v=vs.85%29.aspx [ ^ ]。



P / Invoke等价物已经为您编写:

http://www.pinvoke.net/default.aspx/kernel32.createfile [ ^ ],

http://www.pinvoke.net/default.aspx/kernel32.setfilepointer [ ^ ],

http://www.pinvoke.net/default.aspx/kernel32.readfile [ ^ ],

http://www.pinvoke.net/default.aspx/kernel32/ReadFileEx.html [ ^ ],

http://www.pinvoke.net/default.aspx/kernel32.writefile [ ^ ],

http://www.pinvoke.net/default.aspx/kernel32/WriteFileEx.html [ ^ ]。



对于文件名代表发送整个磁盘,您可以使用系统中注册的驱动器名称,您可以从WMI获取(名称以'\\'开头)。



如果您不想使用物理驱动器而是使用分区,那么您将深入研究所有这些分区系统并解析分区表,引导记录以及所有这些内容。这本身就是一个很大的话题。你可以从这里开始:

http://en.wikipedia.org/wiki/Disk_partitioning [ ^ ],

http://en.wikipedia.org/wiki/Partition_table [ ^ ],

http://en.wikipedia.org/wiki/Master_Boot_Record [ ^ ],

http://en.wikipedia.org/wiki/GUID_Partition_Table [ ^ ],

< a href =http://en.wikipedia.org/wiki/Boot_Engineering_Extension_Record#BEER> http:// en .wikipedia.org / wiki / Boot_Engineering_Extension_Record#BEER [ ^ ]。



现在,您需要的大部分或全部操作可能需要提升您的应用程序的权限。这就是你手动完成的方法:

http://4sysops.com/archives/vista%E2%80%99s-uac-8-ways-how-to-elevate-an-应用程序运行管理员权限 [ ^ ],

http://www.sevenforums.com/tutorials/11841-run-administrator.html [ ^ ]。



实际上,您需要添加一个应用程序清单并将其嵌入到程序集中,以便从一开始就请求提升。这在这里解释:

http://msdn.microsoft.com/en -us / library / bb756929.aspx [ ^ ]。



-SA


creizlein写道:

你好谢尔盖,首先,非常感谢你的回复,它确实给了我一些很好的信息,但也许我自己表达了不好,我没有调查写入和读取文件到磁盘的方式,而不是获取正在读取/写入它们的字节数,非常类似于磁盘监视器的使用。

很抱歉没有回答此请求,但它确实是非常不清楚,首先。



对于这样的事情,你需要类 System.IO.FileSystemWatcher

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx [< a href =http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



你需要注意某些问题。请参阅这些CodeProject文章:

FileSystemWatcher - Pure Chaos(Part 1 of 2) [ ^ ],

FileSystemWatcher - Pure Chaos( 2)的第2部分 [ ^ ]。



-SA


I'm trying to get a per disk ( or per partition ) usage report, aka read/write bytes
i know i can use performance counters for this, or even WMI, but i would like to achieve this same result by using native WinAPI with p/invoke

does anyone can point me to the right functions or libraries to query those details?
i assume i would need a function to return the current partitions and then other that returns total bytes read/write or something like that?

This is to be used in vb.net code, but guess this doesn't matter at this stage.

Regards,

解决方案

creizlein wrote:

you are right, i know that its a dirty road that is not supposed to be taken, using the managed libraries is much better, but i need to avoid them for some other specific reasons, that's the reason i asked for the api functions, and yes, those will be invoked from VB.net, that's why i added the tag, but maybe i should remove it. i apologize if inappropriate..

As I say, you could find appropriate raw Windows API yourself.

Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364417%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363798%28v=vs.85%29.aspx[^].

See also: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364407%28v=vs.85%29.aspx[^].

—SA


Sure. You will need to use low-level disk access, the following raw Windows API:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365541%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365468%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365748%28v=vs.85%29.aspx[^].

The P/Invoke equivalents are already written for you:
http://www.pinvoke.net/default.aspx/kernel32.createfile[^],
http://www.pinvoke.net/default.aspx/kernel32.setfilepointer[^],
http://www.pinvoke.net/default.aspx/kernel32.readfile[^],
http://www.pinvoke.net/default.aspx/kernel32/ReadFileEx.html[^],
http://www.pinvoke.net/default.aspx/kernel32.writefile[^],
http://www.pinvoke.net/default.aspx/kernel32/WriteFileEx.html[^].

For the file names representing the whole disk, you can use the drive names as they are registered in the system, which you can obtain from WMI (the names started with '\\').

If you want to work not with physical drives but with partitions, you will have do delve into all those partitioning systems and parsing of the partition tables, boot records, all that stuff. This is quite a big topic itself. You can start here:
http://en.wikipedia.org/wiki/Disk_partitioning[^],
http://en.wikipedia.org/wiki/Partition_table[^],
http://en.wikipedia.org/wiki/Master_Boot_Record[^],
http://en.wikipedia.org/wiki/GUID_Partition_Table[^],
http://en.wikipedia.org/wiki/Boot_Engineering_Extension_Record#BEER[^].

Now, most or all of the operations you need may require elevated privilege for your application. This is how you do it manually:
http://4sysops.com/archives/vista%E2%80%99s-uac-8-ways-how-to-elevate-an-application-to-run-it-with-administrator-rights[^],
http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

In practice, instead, you will need to add an application manifest and embed it in your assembly to request elevation from the very beginning. This is explained here:
http://msdn.microsoft.com/en-us/library/bb756929.aspx[^].

—SA


creizlein wrote:

Hello Sergey, First off, thank you very much for your reply, it did give me some good information, but maybe i expressed bad myself, I am not looking into ways of writing and reading files to the disks but instead to get the amounts of bytes that are being read/written to them, pretty much like a disk monitor usage.

Sorry for not answering to this request, but it really was pretty unclear, in first place.

For such things, you need the class System.IO.FileSystemWatcher:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx[^].

You need to be aware of certain problems though. Please see these CodeProject articles:
FileSystemWatcher - Pure Chaos (Part 1 of 2)[^],
FileSystemWatcher - Pure Chaos (Part 2 of 2)[^].

—SA


这篇关于使用API​​代码的磁盘读/写字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆