研究制作USB分布式应用程序的可能性,该应用程序
将在插入USB记忆棒时自动启动并在移除记忆棒时关闭
将使用.Net和C#.
寻找建议如何使用C#解决这个问题?
我构建了一个小脚本,用于检查具有给定名称的USB Stick是否已插入计算机,但现在我想围绕此脚本构建一个服务,以观察Stick是否插入.起初我尝试用filewatcher执行此操作并在Stick上创建一个文件,但是如果从PC上取下棒并重新插入filewatcher dosent意识到它.以下脚本检查Stick是否插入,但我需要一个脚本来循环此DriveInfo.GetDrive函数.我不知道最好的方法是在这个函数周围建立一个10秒的定时器循环,或者.NET Framework中是否有可移动设备的观察者类.脚本来了:
public static void Main()
{
Run();
}
public static void Run()
{
var drives = DriveInfo.GetDrives()
.Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
if (drives.Count() > 0)
{
foreach (var drive in drives)
{
if (drive.VolumeLabel == "TESTLABEL") Console.WriteLine("USB Stick is plugged in");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试检测usb到达事件.我试图覆盖wndproc()
以获取我的消息.但我正面临着Windows消息的错误.
错误是:
The name 'WM_DEVICECHANGE' does not exist in the current context
The name 'DBT_DEVICEARRIVAL' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)
这也是我尝试过的代码.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32.SafeHandles;
namespace USBCheckerApp
{
public partial class Form1 : Form
{
bool bDeviceFound = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (!bDeviceFound)
{
button1.Enabled = false;
}
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message …
Run Code Online (Sandbox Code Playgroud)