|
|
Hi All,
Pete O'Hanlon has helped me with this ([^]Yesterday & before) but the thing is I still need a prod in the correct direction, to give the correct and full story (which might make more sense to Pete!) I have to write some software to interface to a board with a USB, I am not happy about installing the driver that installed already and breaking things. I have written successfully some code to check for the driver installed. In thinking that the driver in the future could be updated I wanted to check the date and or revision of the driver so if I install over the old driver only if it's earlier than the one I need.
The code I had was
ServiceController[] scDevices = ServiceController.GetDevices();
MessageBox.Show("Device driver services on the local computer");
foreach (ServiceController scTemp in scDevices)
{
lstDrivers.Items.Add(scTemp.ServiceType + "," + scTemp.ServiceName +
"," + scTemp.DisplayName);
}
This will shove all the installed drivers to a list box another bit of code
index = lstDrivers.FindString("KernelDriver,FTDIBUS,USB Serial Converter Driver");
if (index != -1)
{
lstDrivers.SetSelected(index, true);
}
Using this method using ServiceController.GetDevices() I think the reference Pete gave me yesterday doesn't really help with the issue I was having, I would like to be able to read all the data concerning the drivers date and version at both the install & run time
(thinking it would be useful for another project) I would like to know two things a) how to use the bit of code Pete supplied, b) Should I do the driver as part of the setup or tell the user "if it don't appear install this!"?
Glenn(sorry for being thick!)
modified 5-Jul-12 11:54am.
|
|
|
|
|
Ah. I see what you're trying to get - it's a lot clearer now. Anyway, you might find this code to be more of a match:
class Program
{
static void Main(string[] args)
{
var x = GetDriverInfo("ConfigMgr Remote Control Driver");
}
public class SignedDriver
{
public string DriverVersion;
public string DeviceName;
public string DriverDate;
}
public static SignedDriver GetDriverInfo(string driverName)
{
SignedDriver driver = new SignedDriver();
SelectQuery query = new SelectQuery("Win32_PnPSignedDriver");
query.Condition = string.Format("DeviceName='{0}'", driverName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject manageObject in searcher.Get())
{
object driverVer = manageObject.Properties["DriverVersion"].Value;
object deviceName = manageObject.Properties["DeviceName"].Value;
object driverDate = manageObject.Properties["DriverDate"].Value;
if (deviceName != null)
{
driver.DeviceName = deviceName.ToString();
driver.DriverDate = driverDate.ToString();
driver.DriverVersion = driverVer.ToString();
}
}
return driver;
}
}
|
|
|
|
|
Hi,
Thanks for that (should learn to fully explain problem!) OK to run that piece of code I have pasted into a Console App added a Console.ReadLine(); and added the reference to System Management. The syntax checker has given the blue 'wiggly' line to indicate a reference has not been made (right?).
Glenn
|
|
|
|
|
Have you added using System.Management; at the top of that file?
|
|
|
|
|
No I hadn't, I did and was greeted with a console window (!), now I need to know how to get the gory details out, if use
Console.WriteLine(x.ToString());
I get " Console Application 27.Program+SignedDriver " after a wait which I assume is due to the data being collected, how can I echo the returned data by the
return driver;
I take it the class SignedDriver takes the values that can be printed with
Console.WriteLine("{0}:{1}:{2}"+DriverVersion+DeviceName+DriverDate);
the question is where to put this command to get something useful(!)
Sorry this is turning into a task that is beyond a joke! (I should have stuck to VB6 & C)
Glenn
|
|
|
|
|
Console.WriteLine("{0}:{1}:{2}",x.DriverVersion,x.DeviceName,x.DriverDate); BTW - x is a horrible name for the variable. I only used it in my original sample as a place holder for me to hover over in the debugger to inspect the values. I would rename it to something meaningful if I were you.
|
|
|
|
|
Thanks for that I have got:
ConsoleApplication27.Program+SignedDriver
:: It's printing the place holders meaning I now need to loop with for or while to the end of the list right? as the current x.DeviceName does not have a value in it?
does var x contain all the data? or will another variable have to used?
Glenn
|
|
|
|
|
Do you, or do you not, know the device name of the driver? If you know the name of the driver, put that into GetDriverInfo . That will retrieve a single instance of that driver, which will be fully populated with the DeviceName , DriverVersion and DriverDate .
If you don't, you will need to amend GetDriverInfo to return a list of SignedDriver items, and add a SignedDriver instance whenever you have populated it. You would then loop through this list after you called GetDriverInfo .
|
|
|
|
|
In the grand tradition of "do this" with no details the driver is FTDIBUS I think
I have tried:
var DriverDetails = GetDriverInfo("FTDIBUS");
still prints ::
and various versions shortening the string lengthening it I'm lost...
tried looking around for other sources of how to get the correct driver and nearly all of them just check the name (which I can do!) it just that I can't get to anything useful like a date or version number.
Glenn (Confusion reigns!)
|
|
|
|
|
Get the complete list, as I suggest, and then iterate over it to see which one matches the FTDIBUS. Don't try guesswork, let the computer do the work for you.
|
|
|
|
|
right okay, if I run the console app pretty much as stands how can I get to the class SignedDriver as when I alter the GetDriverInfo line to be
var DriverDetails = GetDriverInfo(SignedDriver);
it gives back: type used like variable, overload method errors, cannot convert to string
This is what I never understood about C++ classes (by the way what are you doing answering silly questions at half 5 in the morning?)
Glenn
|
|
|
|
|
Try this to retrieve a list of all the drivers and display them on the console.
class Program
{
static void Main(string[] args)
{
List<SignedDriver> drivers = GetDriverInfo();
foreach (SignedDriver driver in drivers)
{
Console.WriteLine("Driver name: {0} - Version : [1} - Date {2}",
driver.DeviceName, driver.DriverVersion, driver.DriverDate);
}
Console.WriteLine("Finished processing");
Console.ReadKey();
}
public class SignedDriver
{
public string DriverVersion;
public string DeviceName;
public string DriverDate;
}
public static List<SignedDriver> GetDriverInfo()
{
List<SignedDriver> drivers = new List<SignedDriver>();
SelectQuery query = new SelectQuery("Win32_PnPSignedDriver");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject manageObject in searcher.Get())
{
object driverVer = manageObject.Properties["DriverVersion"].Value;
object deviceName = manageObject.Properties["DeviceName"].Value;
object driverDate = manageObject.Properties["DriverDate"].Value;
if (deviceName != null)
{
SignedDriver driver = new SignedDriver();
driver.DeviceName = deviceName.ToString();
driver.DriverDate = driverDate.ToString();
driver.DriverVersion = driverVer.ToString();
drivers.Add(driver);
}
}
return drivers;
}
} And to answer your question, it's nearly mid day here in the UK.
|
|
|
|
|
Thanks, umm there is an issue (possibly due to my PC!) as it appears to enter the List<signeddriver> GetDriverInfo(), the foreach statements if (if(deviceName != null))
meaing there must be a deviceName as it enters the loop creates an instance of the driver
gets past driver.DeviceName = deviceName.ToString(); and stops driver.DriverDate I think it's due the values of it being null other than the name Multimedia Audio Controller looking in Device Mangler it comes up in the yellow question mark of Other Devices.
If I mod it to
if ((deviceName != null)&&(driverDate !=null)&&(driverVer !=null))
it will get past and fall over at foreach in the main
foreach (SignedDriver driver in drivers)
{
Console.WriteLine("Driver name: {0} - Version : [1} - Date {2}",
driver.DeviceName, driver.DriverVersion, driver.DriverDate);
} it gives an unhandled exception highlighting it in the debugger it is complaining about the input string not being in the correct format but inspecting the values they don't really appear of spec the number of drivers is 85 (increased from 54 0 to 53) Sorry for being thick (btw I am in the UK too it's just your mail was time stamped 05:30)
Glenn
|
|
|
|
|
I have to assume then that some of these values are null, so the call to ToString is the call that's failing. Try this:
driver.DeviceName = deviceName.ToString();
if (driver.DriverDate != null)
driver.DriverDate = driverDate.ToString();
if (driver.DriverVersion != null)
driver.DriverVersion = driverVer.ToString(); In the Console.WriteLine, use something like this:
Console.WriteLine("Driver name: {0} - Version : [1} - Date {2}",
driver.DeviceName, driver.DriverVersion ?? "No version", driver.DriverDate ?? "No date");
|
|
|
|
|
right tried that same issue, I did check with the debugger and I can't "see" anything wrong what was old say cure one bug, cause another? I thought maybe some thing was wrong with the way I positioned the code
foreach (SignedDriver driver in drivers)
{
Console.WriteLine("Driver name: {0} - Version : [1} - Date {2}",driver.DeviceName, driver.DriverVersion ?? "No version", driver.DriverDate ?? "No date");
}
I think its trying display the driver.DriverVersion before(?) "No Version" can be copied into it.
if I put
if (driver.DriverVersion == null)
driver.DriverVersion = "No version";
if (driver.DriverDate == null)
driver.DriverDate = "No Date";
I'm starting to wonder if selling the Big Issue is an option!
[EDIT]
By commenting out the Console.WriteLine() (& removing the use of the program!)
and adding the following
a++;
Console.ReadKey();
Console.WriteLine(a.ToString());
It seems to loop up to 85 produces and then shows the message for correctly finished
Console.WriteLine("Finished processing");
from creeping around the debug screen I can tell its the 85 (85th entry that causes the problem, 0 to 84) could it be due to end of the table not being done properly?
But from creeping around the debug screen I can see the driver I want at location 20 called something different in the
System.Management.ManagementObject manageObject gives the
KernelDriver,FTDIBUS,USB Serial Converter Driver
Which I thought was the driver the name it gives is:
" USB Serial Converter" the DriverVersion gives 2.8.24.0
I'm now fully confused!
modified 5-Jul-12 10:11am.
|
|
|
|
|
I have solved the issue I was having, thank you, I have learned two lessons from this
"Never Use Cut & Paste Code Directly" always look at what you are doing!,
when in stuck "have a coffee, deal with a grateful customer (they do exist at times)"
and then return to piece of code that is causing trouble. What was causing the issue
Console.WriteLine("Driver name: {0} - Version : [1} - Date {2}",
driver.DeviceName, driver.DriverVersion ?? "No version", driver.DriverDate ?? "No date"); I had stupidly copied across not really paying attention [ not {, stupid, stupid, dunces cap on! sorry Pete and thanks for the help answering my stupid questions.
|
|
|
|
|
My apologies for the bum steer. I just knocked this up in the CP message box, so I missed that. Good job though.
|
|
|
|
|
No worries, how do I up vote you as you have gone overboard helping I owe you a beer! (if you are ever in Bristol)
|
|
|
|
|
There's no need to upvote me. I'm not really that bothered about rep points. Simply mark the thread as SOLVED (edit your original post and put SOLVED at the end of the title) and that will do me.
Now, the beer on the other hand...
|
|
|
|
|
|
|
DataSet testvalue = getdata();
testvalue.Relations.Add(new DataRelation("parentChildCategories", testvalue.Tables[0].Columns["lob_parent_id"], testvalue.Tables[0].Columns["lob_id"], false));
TreeView1.DataSource = testvalue;
TreeView1.DataBind();
|
|
|
|
|
|
Was that an existential query?
|
|
|
|