none
怎么判断一个路径是文件夹,但不是磁盘或u盘移动硬盘等其他类型?

    问题

  • 使用拖拽功能,将外部项(文件夹和文件)拽进窗口,使用linq筛选拖拽项的含有的文件和文件夹,我发现使用Directoy.Exist方法筛选文件夹时,能把磁盘判定是文件夹类型,使用DriveInfo.GetDrives()获取磁盘可以解决,但是若用户是拖拽u盘到窗口话,就不能行了,还是会判定是文件夹类型,如何解决这个问题

    var DirectoryList = (from string s in DragList
                                where Directory.Exists(s) == true
                                select s).ToList();
    
                DriveInfo[] a=DriveInfo.GetDrives();
                bool bool1= true;
                foreach(var i in DirectoryList)
                {
                    if (a[0].Name == i)
                    {
                        bool1 = false;
                        break;
                    }
                }


    2017年3月22日 7:36

答案

  • http://stackoverflow.com/questions/27965098/c-sharp-how-do-i-check-usb-ports-with-usb-hub-and-used-controller

    http://stackoverflow.com/questions/4396634/how-can-i-determine-if-a-given-drive-letter-is-a-local-mapped-usb-drive

    System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
    foreach (var drive in drives)
    {
        string driveName = drive.Name; // C:\, E:\, etc:\
    
        System.IO.DriveType driveType = drive.DriveType;
        switch (driveType)
        {
            case System.IO.DriveType.CDRom:
                break;
            case System.IO.DriveType.Fixed:
                // Local Drive
                break;
            case System.IO.DriveType.Network:
                // Mapped Drive
                break;
            case System.IO.DriveType.NoRootDirectory:
                break;
            case System.IO.DriveType.Ram:
                break;
            case System.IO.DriveType.Removable:
                // Usually a USB Drive
                break;
            case System.IO.DriveType.Unknown:
                break;
        }
    }

    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2017年3月22日 10:14

全部回复

  • 参考这里

    http://stackoverflow.com/questions/4396634/how-can-i-determine-if-a-given-drive-letter-is-a-local-mapped-usb-drive

    https://www.codeproject.com/kb/system/drivedetector.aspx

    Detecting USB Drive Removal in a C# Program


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2017年3月22日 10:03
  • http://stackoverflow.com/questions/27965098/c-sharp-how-do-i-check-usb-ports-with-usb-hub-and-used-controller

    http://stackoverflow.com/questions/4396634/how-can-i-determine-if-a-given-drive-letter-is-a-local-mapped-usb-drive

    System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
    foreach (var drive in drives)
    {
        string driveName = drive.Name; // C:\, E:\, etc:\
    
        System.IO.DriveType driveType = drive.DriveType;
        switch (driveType)
        {
            case System.IO.DriveType.CDRom:
                break;
            case System.IO.DriveType.Fixed:
                // Local Drive
                break;
            case System.IO.DriveType.Network:
                // Mapped Drive
                break;
            case System.IO.DriveType.NoRootDirectory:
                break;
            case System.IO.DriveType.Ram:
                break;
            case System.IO.DriveType.Removable:
                // Usually a USB Drive
                break;
            case System.IO.DriveType.Unknown:
                break;
        }
    }

    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2017年3月22日 10:14