Solved

write-protected media error

Posted on 2004-08-12
7
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…

738 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