0

使用下面的代码,命令提示符在出现后几乎立即消失。此代码不应该显示所有正在使用的可移动驱动器吗?

using System.Linq;
using System.IO;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var drives = DriveInfo.GetDrives()
             .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
            Console.WriteLine("Removable drives being used:", drives);
        } 
    }
}

提前谢谢了!

4

3 回答 3

2

最后尝试a Console.ReadLine()。一旦正在运行的应用程序终止,Windows 就会关闭命令窗口。

于 2012-08-14T13:31:13.710 回答
2

因为它执行你的命令并且存在。你可以给它一个指令,让它在关闭之前等待任何按键。

例如:

Console.ReadKey();

更新:

using System.Linq;
using System.IO;
using System;

class Program
{
    static void Main(string[] args)
    {
        DriveInfo[] drives = DriveInfo.GetDrives()
         .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
         foreach(DriveInfo di in drives)
         {
             Console.WriteLine("Removable drives being used:", di.Name);
         {

    } 
}
于 2012-08-14T13:32:22.593 回答
1

试试这个...

 DriveInfo[] allDrives = DriveInfo.GetDrives();
 foreach(DriveInfo dv in drives)          
 {              
        Console.WriteLine("drive Name:{0}", dv.Name);      
 }    
Console.ReadLine();
于 2012-08-14T13:41:51.547 回答