编写一个xml文档来对来自两个不同子例程的信息进行分组vb.net
我正在尝试使用常用信息检索机器上运行的所有进程;进程 ID、名称和内存使用情况。我试图将其全部导出到一个 xml 文件下,但是为了获取所有信息,我必须使用 2 种单独的方法。
为了获取进程 ID、名称和会话 ID(进程所有者),我使用了以下代码以及字典和类,使用了名为 Cassia 的 .net 库。
Using server As ITerminalServer = manager.GetRemoteServer("Spartacus")
server.Open()
For Each process As ITerminalServicesProcess In server.GetProcesses()
If Not String.IsNullOrEmpty(process.ProcessId) Then
dictprocess.Add(process.ProcessId, New Processes(process.ProcessId, process.ProcessName, process.SessionId))
End If
Next
End Using
Dim sbProcess As New StringBuilder
sbProcess.AppendLine("<?xml version=""1.0""?>")
sbProcess.AppendLine("<Processes>")
For Each item As KeyValuePair(Of Integer, Processes) In dictprocess
sbProcess.AppendFormat("<Name>""{0}""<ProcessID>""{1}""</ProcessID><ProcessName>{2}</ProcessName><SessionID>{3}</SessionID></Name>", item.Key, item.Value.ProcessId, item.Value.ProcessName, item.Value.SessionId).AppendLine()
Next
sbProcess.AppendLine("</Processes>")
为了获取内存和页面文件使用情况,我使用了 wmi 查询
Dim sbTest As New System.Text.StringBuilder
objWMI = GetObject("winmgmts:\\.\root\cimv2")
colObjects = objWMI.ExecQuery("Select * From Win32_Process")
For Each Item In colObjects
sbTest.AppendFormat("<ProcessID>""{0}""<ProcessName>""{1}""</ProcessName><MemoryUsage>{2}</MemoryUsage><PageFileUsage>{3}</PageFileUsage></ProcessID>", Item.ProcessID, Item.Name, (Item.WorkingSetSize / 1024), Item.PageFileUsage)
Next
,我可以将结果编写为两个单独的 xml 文件:
进程 id、名称和会话 id:
- <Processes>
- <Name>
"540"
<ProcessID>"540"</ProcessID>
<ProcessName>wininit.exe</ProcessName>
<SessionID>0</SessionID>
</Name>
- <Name>
"552"
<ProcessID>"552"</ProcessID>
<ProcessName>csrss.exe</ProcessName>
<SessionID>1</SessionID>
进程内存和页面文件使用情况
- <ProcessUsage>
- <ProcessID>
"540"
<ProcessName>"wininit.exe"</ProcessName>
<MemoryUsage>4080</MemoryUsage>
<PageFileUsage>1464</PageFileUsage>
</ProcessID>
- <ProcessID>
"552"
<ProcessName>"csrss.exe"</ProcessName>
<MemoryUsage>5600</MemoryUsage>
<PageFileUsage>2332</PageFileUsage>
</ProcessID>
正如您所看到的唯一 id(进程 id ) 在每个 xml 中都是相同的。有什么方法可以编写 xml,以便两种方法的信息都可以在一个标签下吗?
I am trying to retrieve all processes running on a machine with the usual information; process id, name and the memory usage. im trying to export it all under one xml file, however to get all the information i had to use 2 separate methods.
to get the process id, name and session id (process owner) i used the following code along with a dictionary and a class, usng a .net library called Cassia.
Using server As ITerminalServer = manager.GetRemoteServer("Spartacus")
server.Open()
For Each process As ITerminalServicesProcess In server.GetProcesses()
If Not String.IsNullOrEmpty(process.ProcessId) Then
dictprocess.Add(process.ProcessId, New Processes(process.ProcessId, process.ProcessName, process.SessionId))
End If
Next
End Using
Dim sbProcess As New StringBuilder
sbProcess.AppendLine("<?xml version=""1.0""?>")
sbProcess.AppendLine("<Processes>")
For Each item As KeyValuePair(Of Integer, Processes) In dictprocess
sbProcess.AppendFormat("<Name>""{0}""<ProcessID>""{1}""</ProcessID><ProcessName>{2}</ProcessName><SessionID>{3}</SessionID></Name>", item.Key, item.Value.ProcessId, item.Value.ProcessName, item.Value.SessionId).AppendLine()
Next
sbProcess.AppendLine("</Processes>")
in order to get the memory and page file usage i used a wmi query
Dim sbTest As New System.Text.StringBuilder
objWMI = GetObject("winmgmts:\\.\root\cimv2")
colObjects = objWMI.ExecQuery("Select * From Win32_Process")
For Each Item In colObjects
sbTest.AppendFormat("<ProcessID>""{0}""<ProcessName>""{1}""</ProcessName><MemoryUsage>{2}</MemoryUsage><PageFileUsage>{3}</PageFileUsage></ProcessID>", Item.ProcessID, Item.Name, (Item.WorkingSetSize / 1024), Item.PageFileUsage)
Next
I can write the results as two separate xml files:
Process id, name and session id:
- <Processes>
- <Name>
"540"
<ProcessID>"540"</ProcessID>
<ProcessName>wininit.exe</ProcessName>
<SessionID>0</SessionID>
</Name>
- <Name>
"552"
<ProcessID>"552"</ProcessID>
<ProcessName>csrss.exe</ProcessName>
<SessionID>1</SessionID>
Process memory and page file usage
- <ProcessUsage>
- <ProcessID>
"540"
<ProcessName>"wininit.exe"</ProcessName>
<MemoryUsage>4080</MemoryUsage>
<PageFileUsage>1464</PageFileUsage>
</ProcessID>
- <ProcessID>
"552"
<ProcessName>"csrss.exe"</ProcessName>
<MemoryUsage>5600</MemoryUsage>
<PageFileUsage>2332</PageFileUsage>
</ProcessID>
As you can see the unique id (process id) is the same in each xml. Is there any way i can write the xml so that the information from both methods can be under one tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,您可以创建一个类,其中的成员拥有您想要收集的所有数据。然后利用某种类型的容器(我建议使用以进程 ID 为键的字典)修改您的函数以获取所述容器。如果在容器中找到进程 ID,则更新所需的字段;如果未找到,则添加条目。然后循环遍历容器并写入 xml。
In general you can create a class which has members that have all the data you would like to collect. Then utilize some type of container ( I would suggest a dictionary keyed on process id) modify your function to take said container. If the process id is found within the container update the fields you want if it is not found add the entry. Then loop though the container and write the xml.