USB device Set Attribute in C#

Posted by p19lord on Stack Overflow See other posts from Stack Overflow or by p19lord
Published on 2014-07-25T12:51:48Z Indexed on 2014/08/23 22:21 UTC
Read the original article Hit count: 145

Filed under:
|
|
|

I have this bit of code:

DriveInfo[] myDrives = DriveInfo.GetDrives();
foreach (DriveInfo myDrive in myDrives)
{
    if (myDrive.DriveType == DriveType.Removable)
    {
        string path = Convert.ToString(myDrive.RootDirectory);
        DirectoryInfo mydir = new DirectoryInfo(path);
        String[] dirs = new string[] {Convert.ToString(mydir.GetDirectories())};
        String[] files = new string[] {Convert.ToString(mydir.GetFiles())};

        foreach (var file in files)
        {
            File.SetAttributes(file, ~FileAttributes.Hidden);
            File.SetAttributes(file, ~FileAttributes.ReadOnly);
        }

        foreach (var dir in dirs)
        {
            File.SetAttributes(dir, ~FileAttributes.Hidden);
            File.SetAttributes(dir, ~FileAttributes.ReadOnly);
        }        
    }
}

I have a problem. It is trying the code for Floppy Disk drive first which and because no Floppy disk in it, it threw the error The device is not ready. How can I prevent that?

© Stack Overflow or respective owner

Related posts about c#

Related posts about file-io