C# 继续之前,强制安装USB拇指驱动器
我正在用Windows7上的VisualStudio2008在C#上编写一个更新程序。我想让用户插入一个USB拇指驱动器,如果程序发现驱动器和驱动器上的更新,那么它会自动复制它们。我希望在启动时只检查一次,然后执行一个不知道更新的程序(更新确实需要在程序关闭时发生) 我的问题是更新程序是在安装thumb驱动器之前运行的,因此计算机没有检测到thumb驱动器,也没有更新,并且过早地移动。我想让一切尽可能快地运行,但我需要在检测之前强制安装任何thumb驱动器。一切都必须是自动的,没有用户的输入 这在c#中可能吗 编辑更多详细信息: 我目前在启动时运行一个批处理文件(实际上是Windows7shell,但我不确定这会有什么不同)。批处理文件运行更新检查,然后运行实际程序。如果用户的USB驱动器在启动时卡住了,那么我希望更新程序查看驱动器并复制任何新文件 当前代码如下所示:C# 继续之前,强制安装USB拇指驱动器,c#,windows,visual-studio,C#,Windows,Visual Studio,我正在用Windows7上的VisualStudio2008在C#上编写一个更新程序。我想让用户插入一个USB拇指驱动器,如果程序发现驱动器和驱动器上的更新,那么它会自动复制它们。我希望在启动时只检查一次,然后执行一个不知道更新的程序(更新确实需要在程序关闭时发生) 我的问题是更新程序是在安装thumb驱动器之前运行的,因此计算机没有检测到thumb驱动器,也没有更新,并且过早地移动。我想让一切尽可能快地运行,但我需要在检测之前强制安装任何thumb驱动器。一切都必须是自动的,没有用户的输入 这
DriveInfo[] ListDrives = DriveInfo.GetDrives();
foreach (DriveInfo Drive in ListDrives)
{
if(Drive.DriveType == DriveType.Removable)
{
// double check it's valid and copy over stuff
}
}
但它目前在引导时找不到驱动器。如果我以后再运行,那么一切都很好。我假设,由于我这么早运行更新程序,它就没有机会挂载,但如果不需要的话,我不想等待N秒,因为在正常情况下,这只是死区时间
如果我能从它开始轻松地执行此检查,这比必须持续监视事件,然后关闭一切并进行更新要简单得多。您没有给出太多详细信息,但似乎可以调用DriveInfo.GetDrives(),它返回一个DriveInfo[]类型的数组 DriveInfo有一个IsReady()方法。假设在检查驱动器是否准备就绪后,您可以在USB驱动器()上查找已知文件,以验证它们是否安装了正确的USB驱动器
你可以循环轮询,直到找到你想要的,但是如果你在60秒内找不到你想要的,你需要通知用户你找不到你需要的USB驱动器。你没有给出太多细节,但似乎你可以调用DriveInfo.GetDrives(),它返回一个DriveInfo[]类型的数组 DriveInfo有一个IsReady()方法。假设在检查驱动器是否准备就绪后,您可以在USB驱动器()上查找已知文件,以验证它们是否安装了正确的USB驱动器
你可以循环轮询,直到找到你想要的,但是如果你在60秒内没有找到你想要的,你需要通知用户你找不到你需要的USB驱动器。我在if语句中没有看到ready check。根据MSDN: IsReady指示驱动器是否准备就绪。例如,它表示 CD是否在CD驱动器中或可移动存储设备是否在 准备好读/写操作。如果不测试某个驱动器是否正常运行 准备就绪,但尚未准备就绪,将使用DriveInfo查询驱动器 引发一个异常 你在检查IOException吗?我没有看到事件,因此您可能需要旋转等待或挂接到较低级别的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)。我还没有发现任何说明这一点的文档。我在if语句中没有看到就绪检查。根据MSDN:
IsReady指示驱动器是否准备就绪。例如,它表示
CD是否在CD驱动器中或可移动存储设备是否在
准备好读/写操作。如果不测试某个驱动器是否正常运行
准备就绪,但尚未准备就绪,将使用DriveInfo查询驱动器
引发一个异常
你在检查IOException吗?我没有看到事件,因此您可能需要旋转等待或挂接到较低级别的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)。我还没有发现任何文档表明有任何影响。我建议采用如下解决方案:
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));
}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
使用系统线程;
///
///表示包含应用程序入口点的程序类。
///
公共课程
{
///
///表示应用程序的入口点。
///
///可能是指定的命令行参数。
公共静态void Main(字符串[]args)
{
RemovableDriveWatcher rdw=new RemovableDriveWatcher();//创建RemovableDriveWatcher类的新实例。
rdw.NewDriveFound+=NewDriveFound;//连接到“NewDriveFound”事件。
rdw.DriveRemoved+=DriveRemoved;//连接到“DriveRemoved”事件。
rdw.Start();//开始观看。
//在这里做点什么。。。
Console.ReadLine();
rdw.Stop();//停止观看。
}
///
///在找到新驱动器时执行。
///
///t的发送者
/// <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;
}
}
}
}