Solved

write-protected media error

Posted on 2004-08-12
7
393 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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