Solved

write-protected media error

Posted on 2004-08-12
7
388 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
Comment Utility
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
Comment Utility
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
Comment Utility
What if you get the DirectoryInfo for the root of the disk and check if it is ReadOnly (see the FileAttributes.ReadOnly flag)?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:3Mann
Comment Utility
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
Comment Utility
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
Comment Utility
Closed, 200 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

10 Experts available now in Live!

Get 1:1 Help Now