我想要一个控制元素来向我显示正在运行的程序中有关已连接USB设备的更改。搜索后,我设法找到USB设备并将其打印出来。我还处理有关删除和连接的信息。
但是现在有一个问题,当我将两个部分组合起来时它不起作用。 这是我的代码:
namespace usbPortAbfrage
{
public partial class Form1 : Form
{
ArrayList result = new ArrayList();
public Form1()
{
InitializeComponent();
bla();
}
public ArrayList GetComFriendlyNames()
{
ArrayList names = new ArrayList();
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT InstanceName, PortName FROM MSSerial_PortName");
foreach (ManagementObject port in searcher.Get())
{
names.Add(port["PortName"]);
}
}
catch (ManagementException)
{
}
return names;
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.WParam.ToInt32() == 0x8004)
{
MessageBox.Show("Gerät entfernt");
richTextBox1.Clear();
bla();
}
if (m.WParam.ToInt32() == 0x8000)
{
MessageBox.Show("Gerät angeschlossen");
richTextBox1.Clear();
bla();
}
}
public void bla()
{
richTextBox1.Clear();
ArrayList test = GetComFriendlyNames();
foreach (string name in test)
{
richTextBox1.AppendText(name + "\n");
}
}
}
}
答案 0 :(得分:0)
这是我的测试中的调试代码,试图复制我似乎无法点击事件代码的问题。 (I.E没有这个代码不起作用,这不是答案/解决方案,但我不能将其纳入评论,请不要将其投票!)
public partial class Form1 : Form
{
private const int WM_ACTIVATEAPP = 0x001C;
private const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
private const int DBT_DEVICEREMOVEPENDING = 0x8003; // about to remove, still available
private const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
private bool appActive = true;
public Form1()
{
this.Size = new System.Drawing.Size(300, 300);
this.Text = "Form1";
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
}
protected override void OnPaint(PaintEventArgs e)
{
// Paint a string in different styles depending on whether the
// application is active.
if (appActive)
{
e.Graphics.FillRectangle(SystemBrushes.ActiveCaption, 20, 20, 260, 50);
e.Graphics.DrawString("Application is active", this.Font, SystemBrushes.ActiveCaptionText, 20, 20);
}
else
{
e.Graphics.FillRectangle(SystemBrushes.InactiveCaption, 20, 20, 260, 50);
e.Graphics.DrawString("Application is Inactive", this.Font, SystemBrushes.ActiveCaptionText, 20, 20);
}
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
// Listen for operating system messages.
switch (m.Msg)
{
// The WM_ACTIVATEAPP message occurs when the application
// becomes the active application or becomes inactive.
case WM_ACTIVATEAPP:
// The WParam value identifies what is occurring.
appActive = (((int)m.WParam != 0));
// Invalidate to get new text painted.
this.Invalidate();
break;
case DBT_DEVICEARRIVAL:
MessageBox.Show("Connected!");
break;
case DBT_DEVICEREMOVECOMPLETE:
MessageBox.Show("Disconnected!");
break;
}
base.WndProc(ref m);
}
}
链接到MSDN上的类似问题:http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ea183afd-d070-4abd-8e00-a1784fdfedc5/