Pramod kumar
Pramod kumar

Reputation: 23

How to suppress the disk cannot be written to because it is write protected. Please remove the write protection from the volume in drive in C#

If my flash drive is write protected, I am facing following issue

Scenario:
Directory.Create() gives the message as mentioned below before throwing IOException. But I want to avoid this pop message and handle it in IOException catch block

The pop up says:
The disk cannot be written to because it is write protected. Please remove the write protection from the volume in drive.

How to suppress this pop up message and handle it in IOException catch block ?

Any help is appreciated

Thanks, Manoj

Upvotes: 0

Views: 679

Answers (1)

asdf_enel_hak
asdf_enel_hak

Reputation: 7630

You can use either Exception class with relevant message or implement your own Exception class such as MyException()

try{
    Directory.Create(...);
}catch (IOException ex){
    throw new Exception("Directory cannot be created.", ex);
}


[Serializable]
public class MyException: Exception
{
    public MyException()
    {
    }

    public MyException(string message)
        : base(message)
    {
    }

    public MyException(string message, Exception inner)
        : base(message, inner)
    {
    }
}

Upvotes: 1

Related Questions