The code below:
For Each queryObj As ManagementObject In searcher.Get()
objWriter = New StreamWriter("C:\vsl\scripts\results.txt", False)
objWriter.Write("Active开发者_StackOverflow社区: {0} ", queryObj("Active"))
objWriter.Write(" InstanceName: {0} ", queryObj("InstanceName"))
objWriter.Write(" PredictFailure: {0} ", queryObj("PredictFailure"))
objWriter.Write(" Reason: {0} ", queryObj("Reason"))
objWriter.Close()
Next
Seems to fetch data from my one and only internal IDE drive.
Two things that I don't understand.
The line objWriter.Write("Active: {0} ", queryObj("Active")) does not work if you change up the data in between the first set of quotes. Thus changing Active: {0} to Active: {1} causes an error that the Try Catch doesn't catch (this for...next is in a try catch). I assumed that the stuff inside the quotes was just text and could be changed to anything? Why doesn't the try catch catch it?
Doesn't read a USB external drive when attached. That's how I found out about item 1 above... I changed {0} to {1} and it crashes.
You need to post some more code to help us with #2, for instance how searcher
is defined.
But your first question is pretty simple. The string {0}
is standard replacement syntax. If you wrote this instead:
objWriter.Write("Hello {0}, my name is {1}", "Alice", "Bob")
It would write out:
Hello Alice, my name is Bob
The {0}
represents the first item after the initial string (in programming numbers start at zero, so the first is zero, the second is one, etc). In your case, {0}
is getting replaced by whatever queryObj("Active")
returns.
You can usually have as many {X}
as you want as long as you have values to back them up. Having more values is okay, have too few is an error which is what you ran into.
So this is okay because the last one ("Bob") just gets ignored:
objWriter.Write("Hello {0}", "Alice", "Bob")
But this is not okay because we have nothing to fill in for {1}
objWriter.Write("Hello {0}, my name is {1}", "Alice")
精彩评论