真诚求助一个困扰了很久的问题
下面是一个VBS脚本,我想转换成C#,怎么写?'Script File Name: DiskMonitor.vbs
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colDisks = objRefresher.AddEnum(objWMIService, "Win32_PerfFormattedData_PerfDisk_LogicalDisk").objectSet
If Wscript.Arguments.Count = 0 Then
objRefresher.Refresh
For Each objDisk in colDisks
Wscript.Echo objDisk.Name & " " & objDisk.DiskReadBytesPerSec & " " & objDisk.DiskWriteBytesPerSec
Next
End If
If Wscript.Arguments.Count = 1 Then
Interval = CInt(Wscript.Arguments(0)) * 1000
Do While True
objRefresher.Refresh
Wscript.Echo
Wscript.Echo "Time: " & " " & Time()
Wscript.Echo FormatStr("Device:", 15, 0) & FormatStr("tps", 7, 1) & FormatStr(" kB_read/s", 13, 1) & FormatStr("kB_wrtn/s", 13, 1) & FormatStr("Free Space", 13, 1)
For Each objDisk in colDisks
Wscript.Echo FormatStr(objDisk.Name, 15, 0) & FormatStr(objDisk.DiskTransfersPerSec, 7, 1) & FormatStr(objDisk.DiskReadBytesPerSec, 13, 1) & FormatStr(objDisk.DiskWriteBytesPerSec, 13, 1) & FormatStr(objDisk.PercentFreeSpace & "%", 13, 1)
Next
Wscript.Sleep Interval
Loop
End If
If Wscript.Arguments.Count = 2 Then
i = 0
Interval = CInt(Wscript.Arguments(0)) * 1000
Count = CInt(Wscript.Arguments(1))
Do While i < Count
objRefresher.Refresh
Wscript.Echo
Wscript.Echo "Time: " & " " & Time()
Wscript.Echo FormatStr("Device:", 15, 0) & FormatStr("tps", 7, 1) & FormatStr(" kB_read/s", 13, 1) & FormatStr("kB_wrtn/s", 13, 1) & FormatStr("Free Space", 13, 1)
For Each objDisk in colDisks
Wscript.Echo FormatStr(objDisk.Name, 15, 0) & FormatStr(objDisk.DiskTransfersPerSec, 7, 1) & FormatStr(objDisk.DiskReadBytesPerSec, 13, 1) & FormatStr(objDisk.DiskWriteBytesPerSec, 13, 1) & FormatStr(objDisk.PercentFreeSpace & "%", 13, 1)
Next
Wscript.Sleep Interval
i = i + 1
Loop
End If
Function FormatStr(str, tLen, direction)
sLen = Len(str)
fStr = ""
num = tLen - sLen
j = 0
Do While j < num
fStr = fStr & " "
j = j + 1
Loop
If direction = 1 Then
fStr = fStr & str
Else
fStr = str & fStr
End If
FormatStr = fStr
End Function
--------------------编程问答-------------------- 如果vb也不会,先学vb,然后再学c# --------------------编程问答-------------------- 写c#不一定要有VB基础才行吧?
我用过C#写,但是取出来的数据不理想,不知道错在哪里,以下是大概代码
ManagementObjectSearcher sql = new ManagementObjectSearcher("SELECT * FROM Win32_PerfFormattedData_PerfDisk_LogicalDisk");
ManagementObjectCollection queryCollection = sql.Get();
double r = 0;
double w = 0;
double s = 0;
foreach (ManagementObject mo in queryCollection)
{
DiskIfo d = new DiskIfo();
d.name= mo["Name"].ToString();
d.SDiskReadBytesPerSec = double.Parse(mo["DiskReadBytesPerSec"].ToString());
d.SDiskWriteBytesPerSec = double.Parse(mo["DiskWriteBytesPerSec"].ToString());
d.SDiskTransfersPerSec = double.Parse(mo["DiskTransfersPerSec"].ToString());
r = d.SDiskReadBytesPerSec - d.DiskReadBytesPerSec;
w = d.SDiskWriteBytesPerSec - d.DiskWriteBytesPerSec;
s = d.SDiskTransfersPerSec - d.DiskTransfersPerSec;
d.DiskReadBytesPerSec = d.SDiskReadBytesPerSec;
d.DiskWriteBytesPerSec = d.SDiskWriteBytesPerSec;
d.DiskTransfersPerSec = d.SDiskTransfersPerSec;
} --------------------编程问答-------------------- 源码都有了,只是语言及结构稍作改变就可以了。 --------------------编程问答-------------------- 就是下面几句不知道怎么编写
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colDisks = objRefresher.AddEnum(objWMIService, "Win32_PerfFormattedData_PerfDisk_LogicalDisk").objectSet
微软官方说明不能使用select来查询数据的,我上面的代码就用到了,VBS里面就没有用到
(1)不能使用 objWMIService.ExecQuery 执行 Select 语句来获取磁盘性能数据
(2)必须使用 WbemScripting.SWbemRefresher 将 Win32_PerfFormattedData_PerfDisk_LogicalDisk 加入,然后不断调用 Refresh 方法刷新数据来获取性能信息
(3)第一次刷新的时候,并不能获取有用的数据,从第二次开始,才能获取到磁盘性能数据
(4)以上问题与 WMI 中性能监控使用计数器的机制有关 --------------------编程问答-------------------- http://www.developerfusion.com/tools/convert/vb-to-csharp/
直接把VB代码贴进去,然后按"Convert to C#" --------------------编程问答-------------------- 上面的代码是VBS不是VB哦 --------------------编程问答-------------------- 哦,我看错了.....lol
慢慢改把,存支持
看看这个
There are no automatic tools to do this, as far as I know. There was a Migration Assistant for converting classic asp files over to ASP.NET 1.1, but it wasn't very good, and wasn't even updated to version 2.0.
Frankly, the ASP.NET model is quite different to the classic ASP model, so my advice is to just learn ASP.NET using C# as a completely new thing. Once you have got to grips with it, you can create ASP.NET pages from scratch that contain the same functionality as your classic asp pages.
--------------------编程问答-------------------- 看来只能慢慢改了 --------------------编程问答-------------------- my vbs is so poor.......i can't do it --------------------编程问答-------------------- so.....rewrite it in C# then.... if you know what it does..... --------------------编程问答-------------------- 上面的VBS写的是使用C#来控制WMI获取磁盘I/O速率的,我找了很多资料才找到这个方法,只能死等这个方法了 --------------------编程问答-------------------- 友情帮顶! --------------------编程问答--------------------
帮忙顶也是一种美,谢谢 --------------------编程问答--------------------
大哥VBS跟VB根本不搭界啊 --------------------编程问答--------------------
哥们,your English is so well,let's make friends。 --------------------编程问答--------------------
sure...but it's so 易做图 what i said
my e is so poor that i have none word when i continue --------------------编程问答-------------------- 看看MSDN里的等效语言.
补充:.NET技术 , C#