Solved

write-protected media error

Posted on 2004-08-12
7
389 Views
Last Modified: 2008-02-01
Hi all,

Is there a way to detect programmatically if a media is write-protected or not?
If this is impossible, is there a way to diable the dialog box that pops-up when i write/delete in write-protected media?

Im using C# and in my prog, when I do write functions (set attributes, delete, rename, etc) on files/directories in a write-protected media, i get a dialog box saying that the media is write-protected with cancel, retry and abort buttons.  I want to get rid of this thats why im asking the question above.

Thanks
0
Comment
Question by:3Mann
  • 3
  • 2
7 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11782938
Try creating a simple file on the media and writing something on it programmatically. If it fails - write protected.
0
 
LVL 1

Author Comment

by:3Mann
ID: 11783450
Yes, it fails.
I am able to catch an exception.  But i only am able to get the exception after the cancel, retry or abort dialog box is done.
How do i prevent the dialog box from appearing?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11783735
What if you get the DirectoryInfo for the root of the disk and check if it is ReadOnly (see the FileAttributes.ReadOnly flag)?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:3Mann
ID: 11784086
FileAttributes.ReadOnly checks only for the ReadOnly attribute of the file/directory.
Anyway, even if the media is write-protected, fileattribute returned for the root is Directory.

BTW, my media is SD memory card and SD reader/writer is connected via USB2.0 port.
0
 
LVL 1

Author Comment

by:3Mann
ID: 11939135
public bool isWriteProtected()
{
      bool bRet = false;
      uint uOrigErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);

      try
      {
            //attempt to write to media
            string strTempDir = m_strDriveLetter + @"\forWPChk";
            Directory.CreateDirectory(strTempDir);
            if(Directory.Exists(strTempDir))
                  Directory.Delete(strTempDir);
      }
      catch(IOException)
      {
            bRet = true;
      }
      finally
      {
            SetErrorMode(uOrigErrMode);
      }
      return bRet;
}
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 11987724
Closed, 200 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now