继续之前强制USB拇指驱动器进行安装

我在Windows 7上使用Visual Studio 2008编写了一个更新程序。我希望用户插入USB拇指驱动器,如果程序在驱动器上find驱动器和更新,则会自动将其复制过来。 我希望在启动时只检查一次,然后执行一个不知道更新的程序(在程序closures时确实需要更新)。

我的问题是更新程序正在安装拇指驱动器之前运行,以便计算机检测到没有拇指驱动器,没有更新,并提前移动。 我想尽可能快地运行,但是我需要强制任何拇指驱动器在检测之前安装。 一切都必须是自动的,没有用户的input。

这是可能的在C#?

编辑更多的细节:

我目前在启动时运行batch file(实际上是Windows 7的shell,但我不知道这是否有所作为)。 batch file运行更新检查,然后运行实际的程序。 如果用户在启动时插入了USB驱动器,我希望更新程序查看驱动器并复制新文件。

当前编码看起来像:

DriveInfo[] ListDrives = DriveInfo.GetDrives(); foreach (DriveInfo Drive in ListDrives) { if(Drive.DriveType == DriveType.Removable) { // double check it's valid and copy over stuff } } 

但目前在启动时没有find驱动器。 如果我以后运行,那么一切都很好。 我假设,因为我这么早运行更新,它只是没有机会挂载,但我不想等待N秒,如果我不需要,因为在正常情况下,这只是死亡时间。

如果我能做到这一点,从容易起来,要比持续监视一个事件,然后closures所有东西,做一个更新简单得多。

我会建议像下面这样的解决方案:

 using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Threading; /// <summary> /// Represents our program class which contains the entry point of our application. /// </summary> public class Program { /// <summary> /// Represents the entry point of our application. /// </summary> /// <param name="args">Possibly spcified command line arguments.</param> public static void Main(string[] args) { RemovableDriveWatcher rdw = new RemovableDriveWatcher(); // Create a new instance of the RemoveableDriveWatcher class. rdw.NewDriveFound += NewDriveFound; // Connect to the "NewDriveFound" event. rdw.DriveRemoved += DriveRemoved; // Connect to the "DriveRemoved" event. rdw.Start(); // Start watching. // Do something here... Console.ReadLine(); rdw.Stop(); // Stop watching. } /// <summary> /// Is executed when a new drive has been found. /// </summary> /// <param name="sender">The sender of this event.</param> /// <param name="e">The event arguments containing the changed drive.</param> private static void NewDriveFound(object sender, RemovableDriveWatcherEventArgs e) { Console.WriteLine(string.Format("Found a new drive, the name is: {0}", e.ChangedDrive.Name)); } /// <summary> /// Is executed when a drive has been removed. /// </summary> /// <param name="sender">The sender of this event.</param> /// <param name="e">The event arguments containing the changed drive.</param> private static void DriveRemoved(object sender, RemovableDriveWatcherEventArgs e) { Console.WriteLine(string.Format("The drive with the name {0} has been removed.", e.ChangedDrive.Name)); } } 

RemoveableDriveWatcher类看起来像这样:

 /// <summary> /// Repesents a watcher class for removable drives. /// </summary> public class RemovableDriveWatcher { /// <summary> /// Represents the watcher thread which watches for new drives. /// </summary> private Thread watcherThread; /// <summary> /// Continas all found logical drives of this system. /// </summary> private List<DriveInfo> foundDrives; /// <summary> /// Initializes a new instance of the <see cref="RemovableDriveWatcher"/> class. /// </summary> public RemovableDriveWatcher() { this.foundDrives = new List<DriveInfo>(); this.watcherThread = new Thread(new ThreadStart(ScanLogicalDrives)); this.WaitBetweenScansDelay = 1000; } /// <summary> /// Is fired if a new drive has been detected. /// </summary> public event EventHandler<RemovableDriveWatcherEventArgs> NewDriveFound; /// <summary> /// Is fired if a drive has been removed. /// </summary> public event EventHandler<RemovableDriveWatcherEventArgs> DriveRemoved; /// <summary> /// Gets or sets the delay in ms between two scans. /// </summary> public int WaitBetweenScansDelay { get; set; } /// <summary> /// Starts the watcher. /// </summary> public void Start() { if (!this.watcherThread.IsAlive) { this.watcherThread.Start(); } } /// <summary> /// Stops the watcher. /// </summary> public void Stop() { if (this.watcherThread.IsAlive) { this.watcherThread.Abort(); this.watcherThread.Join(); } } /// <summary> /// Scans for logical drives and fires an event every time a new /// drive has been found or a drive was removed. /// </summary> private void ScanLogicalDrives() { DriveInfo[] drives; do { drives = DriveInfo.GetDrives(); // Check for new drives foreach (DriveInfo drive in drives) { if (!(drive.DriveType == DriveType.Removable)) { continue; } if (!drive.IsReady) { continue; } if (!this.foundDrives.ContainsWithName(drive)) { this.foundDrives.Add(drive); if (this.NewDriveFound != null) { this.NewDriveFound(this, new RemovableDriveWatcherEventArgs(drives, drive)); } } } // Check for removed drives for (int i = this.foundDrives.Count - 1; i >= 0; i--) { DriveInfo drive = this.foundDrives[i]; if (!drives.ContainsWithName(drive)) { if (this.DriveRemoved != null) { this.DriveRemoved(this, new RemovableDriveWatcherEventArgs(drives, drive)); } this.foundDrives.RemoveWithName(drive); } } // Sleep Thread.Sleep(this.WaitBetweenScansDelay); } while (true); } } 

对于一切工作,你需要RemovableDriveWatcherEventArgs:

 /// <summary> /// Represents the RemovableDriveWatcherEventArgs /// </summary> public class RemovableDriveWatcherEventArgs : EventArgs { /// <summary> /// Initializes a new instance of the <see cref="RemovableDriveWatcherEventArgs"/> class. /// </summary> /// <param name="allDrives">All currently available logical drives in the system.</param> /// <param name="changedDrive">The changed drive.</param> public RemovableDriveWatcherEventArgs(DriveInfo[] allDrives, DriveInfo changedDrive) { this.Drives = allDrives; this.ChangedDrive = changedDrive; } /// <summary> /// Gets the changed logical drive that has either been detected or removed. /// </summary> public DriveInfo ChangedDrive { get; private set; } /// <summary> /// Gets all currently available logical drives. /// </summary> public DriveInfo[] Drives { get; private set; } } 

当然,扩展:

 /// <summary> /// Contains extensions used by the RemovableDriveWatcher class. /// </summary> public static class RemovableDriveWatcherExtensions { /// <summary> /// Extends the DiveInfo[] by the ContainsWithName method. /// </summary> /// <param name="all">The array where we want to find the specified instance.</param> /// <param name="search">The instance which we want to find in the array.</param> /// <returns>TRUE if the specified instance was found, FALSE if the specified instance was not found.</returns> public static bool ContainsWithName(this DriveInfo[] all, DriveInfo search) { for (int i = 0; i < all.Length; i++) { if (all[i].Name == search.Name) { return true; } } return false; } /// <summary> /// Extends the List<DriveInfo> by the ContainsWithName method. /// </summary> /// <param name="all">The array where we want to find the specified instance.</param> /// <param name="search">The instance which we want to find in the list.</param> /// <returns>TRUE if the specified instance was found, FALSE if the specified instance was not found.</returns> public static bool ContainsWithName(this List<DriveInfo> all, DriveInfo search) { for (int i = 0; i < all.Count; i++) { if (all[i].Name == search.Name) { return true; } } return false; } /// <summary> /// Extends the List<DriveInfo> by the RemoveWithName method. /// </summary> /// <param name="all">The array where we want to removed the specified instance.</param> /// <param name="search">The instance which we want to remove in the list.</param> public static void RemoveWithName(this List<DriveInfo> all, DriveInfo search) { for (int i = 0; i < all.Count; i++) { if (all[i].Name == search.Name) { all.RemoveAt(i); return; } } } } 

我希望这有一点帮助。

你不会给出太多的细节,但是你可能会调用DriveInfo.GetDrives(),它返回一个DriveInfo []类型的数组

DriveInfo有一个IsReady()方法。 据推测,一旦你检查驱动器准备就绪,你可以在USB驱动器()上查找一个众所周知的文件,以验证他们已经安装了正确的USB

你可以循环查询,直到你找到你想要的,但是如果在60秒内没有找到你想要的东西,你将需要通知用户你找不到你需要的USB驱动器。

我在if语句里看不到一个准备好的检查。 根据MSDN:

IsReady表示驱动器是否准备就绪。 例如,它指示CD是在CD驱动器中还是在可移动存储设备准备好读/写操作。 如果您未测试某个驱动器是否准备就绪,且尚未准备就绪,则使用DriveInfo查询该驱动器将引发IOException。

你在检查IOException吗? 我没有看到IsReady事件,因此您可能需要等待或挂接到较低级别的Windows API才能找到指示驱动器准备就绪的事件。 这里有一个想法:

 try { DriveInfo[] ListDrives = DriveInfo.GetDrives(); foreach (DriveInfo Drive in ListDrives) { if(!Drive.IsReady)//spin if(Drive.DriveType == DriveType.Removable) { // double check it's valid and copy over stuff } } } catch(IOException ex)//... 

我现在没有办法测试这个。 请让我知道它是如何运作的,或者如果有更多的细节我需要注意。

但是,因为您在启动时启动了此过程,所以IsReady总是有可能不够用,而且您可能还需要另外找到其他的东西(我想象的是Windows API)。 我还没有发现任何文件说什么的效果。