2

I am working on determining if a removable drive is read-only using C# (.NET 4.0). I've read many articles that detail the process for determining if a directory is read-only using the following code that came from this page:

var di = new DirectoryInfo(folderName);

if(di.Exists)
{
    if (di.Attributes.HasFlag(FileAttributes.ReadOnly))
    {
        //IsReadOnly...
    }
}

However, I've tried this method using the root directory, and even though the entire drive is read-only (I'm using an SD Card that is locked), I never make it to the line //IsReadOnly.... The contents of the drive cannot be guaranteed (it could be empty or have many, many files), so testing any sub-directory is out of the question. I know an easy test would be to write a small file to the drive and catch any exceptions, but I would prefer a different method.

Does anyone know of a good solution to this problem?

Community
  • 1
  • 1
  • 2
    How are you not getting an error. (di.Exists()) should be (di.Exists). Are you sure folderName Exists ? Make sure there is not a problem with the path. – deathismyfriend Oct 29 '14 at 16:39
  • are you familiar with `WMI` if not here is some good reading as well as a good place to start [Win32_LogicalDisk Class](http://msdn.microsoft.com/en-us/library/aa394173(v=vs.85).aspx) – MethodMan Oct 29 '14 at 16:40
  • Ah, yes, I missed the problem with di.Exists() in the code. I don't actually check to make sure it exists in my code, because I get the drive list from a LINQ query using DriveInfo.GetDrives(). I'll change the code to be correct. – BryanWallin Oct 29 '14 at 16:46

0 Answers0