|
Thanks Kim! I went through the WMI hardware tutorial and was able to run the sample code, but it listed every piece of hardware in the PC except for the one I was interested in, i.e. a USB Input Device under Human Interface Devices. The HID device has this ID: USB\VID_0000&PID_0000&REV_0100. Any idea why I can't communicate with the device using WMI?
Thanks again,
Mart
|
|
|
|
|
hello guys.. I want to copy a list which contains key/values. Now I am unable to sort that list, so I thought to copy that list into KeyValuePair() and the sort this. Here is the list
List<Students> list = students.ToList<Students>()
Now I want to copy this into KeyValuePair(). How can I do that? thnx
|
|
|
|
|
You can't, per-se, copy a list into a KeyValue pair because you are missing the key part of the KVP. You're going to have to iterate over the list and add it into the KVP one value at a time (using an appropriate element to create the key).
|
|
|
|
|
Students already contains name value pairs like name, degree. I am not able to get the idea how to sort that list which already contains name/values pair.
|
|
|
|
|
So, you have a list of Students (btw, your class should really be called Student as it maps to a single student) and you want to sort on the data, e.g. the student Name. What you could do, using Linq for instance:
var output = students.OrderBy(student => student.Name);
|
|
|
|
|
|
If the only reason you want to put it in a KVP is to sort it, you'd be better off having Students (by the way, it should probably be 'Student' unless that class is itself a wrapper for a group of students) implement IComparable and just calling List.Sort. (Or, if you don't own Students, writing a IComparator<Students>.)
|
|
|
|
|
Students already contains name value pairs like name, degree. I am not able to get the idea how to sort that list which already contains name/values pair.
|
|
|
|
|
So do you want to sort the students, or do you want to mix up all the internal name/value pairs of all the students and sort that? Please provide a simple sample (3 students or something) and tell us what your expected output should be.
|
|
|
|
|
This[^] should give you enough information to sort your list of students in any way you like.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
I have a context menu with 11 items on it, and I want to use a single event handler that will catch a click on any one of those menu items. Then I want to know which menu item was clicked and get its text value. I know how to write methods to catch the click event for each menu item individually, but surely I don't have to write a single method for each menu item click, right? This is what I know how to do for a single menu item:
private void menuItem10_Click(object sender, EventArgs e)
{
locationButton.Text = menuItem10.Text;
locationButton.BackColor = System.Drawing.Color.Blue;
}
This is what I'd like to do, but I don't know how to have a single event handler with each menu item registered to it. In other words, can I have a single event handler (we'll call it 'MenuClick') that listens for a click on any one of the menu items in the context menu? Do I have to register each one line by line or can I use a loop?
private void MenuClick(object sender, EventArgs e)
{
System.Windows.Forms.MenuItem clickedMenuItem = sender as System.Windows.Forms.MenuItem;
if (clickedMenuItem != null)
{
locationButton.Text = clickedMenuItem.Text;
locationButton.BackColor = System.Drawing.Color.Blue;
}
}
To register each one, it's like this:
this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
To register each one individually seems a bit overkill and I'm looking for a loop instead. But I don't see how you can dynamically name and register a menuItem object using a variable. In other words, I don't see this as being valid:
for (i = 0; i < 12; i++)
{
this.menuItemi.Click += new System.EventHandler(this.menuItemi_Click);
}
|
|
|
|
|
namelkcip wrote: and get its text value
That's a bad idea.
namelkcip wrote: .menuItemi_Click
You can do that if you make an array of items.
namelkcip wrote: new System.EventHandler
We haven't had to do that in many years.
|
|
|
|
|
if you want each MenuItem of some Menu to have the same click handler, use a simple loop like this:
foreach(MenuItem mi in myMenu.MenuItems) mi.Click+=menuItem_Click;
BTW: mi.Click+=menuItem_Click; is shorthand notation for mi.Click += new System.EventHandler(this.menuItem_Click);
Relying on the MenuItem's text isn't in accordance with defensive programming rules, as you might later on change the item's text (e.g. by introducing Internationalization) and break your code. A better way might be to attach some information (the reference to some object typically) to each of the MenuItems' Tag property, which is available for exactly such purposes.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
modified on Friday, June 10, 2011 8:44 AM
|
|
|
|
|
Is it possible to do this?
Here is my test function which does work locally. How can I get these values remotely, I haven't been able to find much on the net?
Thanks for reading.
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
MessageBox.Show(adapter.Name);
MessageBox.Show(adapter.OperationalStatus.ToString());
MessageBox.Show(adapter.Description);
MessageBox.Show(adapter.NetworkInterfaceType.ToString());
MessageBox.Show(adapter.Speed.ToString());
MessageBox.Show(adapter.GetIPProperties().UnicastAddresses[0].Address.ToString());
}
|
|
|
|
|
GetAllNetworkInterfaces() is specifically for the local computer, so unless you run it on the remote computer and relay the information back some way, then you can't use it.
You may be able to use WMI if it provides the information you need and the network security configuration allows it. See the WMI "Win32_NetworkAdapter" class...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark,
I did look at that class and I am very unfamiliar with WMI, I wasn't able to get much of anywhere with it ... here is what I tried, the only part I wasn't sure of is which values to search for after it queries the database.
public static string getNetworkCardInformation(string serverName)
{
string networkCardInformation = "";
try
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope(GetStandardPath(serverName), options);
scope.Path = new ManagementPath(@"\\" + serverName + @"\root\cimv2");
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Connect();
ObjectQuery query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
using (ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query))
{
foreach (ManagementObject service in search.Get())
{
networkCardInformation = service["IP address="].ToString();
}
}
}
catch (ManagementException ex)
{
networkCardInformation = "ERROR : " + ex.Message;
}
catch (COMException exc)
{
networkCardInformation = "ERROR : " + exc.Message;
}
catch (Exception e)
{
networkCardInformation = "ERROR : " + e.Message;
}
return(networkCardInformation);
|
|
|
|
|
Hi,
here is a code snippet I use to discover the content of some WMI class on the local machine; it tells what keys exist together with their value:
string clas="Win32...";
ManagementObjectSearcher searcher=new ManagementObjectSearcher("SELECT * FROM "+clas);
ManagementObjectCollection results=searcher.Get();
log("There are "+results.Count+" items in "+clas);
int count=0;
foreach (ManagementObject result in results) {
log("=============== "+clas+"["+count+"] ===============");
count++;
PropertyDataCollection pdc=result.Properties;
foreach (PropertyData pd in pdc) {
string name=pd.Name;
object obj=pd.Value;
if (obj!=null) {
string[] sa=obj as string[];
if (sa!=null) {
string ss=string.Join("\";\"", sa);
log(count.ToString()+": "+name+" = string[] = \""+ss+"\"");
} else {
log(count.ToString()+": "+name+" = \""+obj.ToString()+"\"");
}
}
}
}
You could adapt it to also run remotely and only report the key,value pairs you're interested in.
PS: I'm sure the keys don't end on an equal sign!
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
Thank you very much, I will try that tomorrow when I get back to work.
I just started using WMI today, so the equals sign thing, lol
|
|
|
|
|
You're welcome.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.4
|
|
|
|
|
|
Thank you Mark, that looks like something I can adapt right to what I already have!
I'll try it tomorrow and report back.
|
|
|
|
|
hello guys... I dont know where to put this question. Lets say I have made a notepad in C#.NET. Now how can I run that notepad on linux platform? thnx
|
|
|
|
|
You'd need a version of .NET that runs on Linux (obviously). Fortunately, there are Linux versions around, the most popular is called Mono.
|
|
|
|
|
|
Hi,
Have written a small Windows service which monitors a single folder waiting for simple text files to arrive in said folder. Once a file or files arrive, the service converts them to XML, deletes the file(s) from the 'in' folder, copies it to an 'out' folder where another process deals with them. It's a simple, clean service which runs fine 99% of the time.
However, the other 1% of the time, a file or files arrives in the 'in' folder and nothing happens. The service doesn't seem to have noticed that anything has arrived in the folder it's watching.
Does anyone know of any reason why the service would stall every so often. It's a very small problem but one that I'd like to iron out if I can....
Thanks in advance
Scott
|
|
|
|