How To Remove ReadOnly Attribute In C#

ReadOnly Attribute Is Applied To A File So That The File Can't Be Modified.

Removing ReadOnly Attribute Using C#

Lets Create A New C# Windows Form Project In Visual Studio. Add One TextBox (textbox1) To Accept FileName With Complete Path & One Button (Button1) Upon Clicking Which The Process Of Removing The ReadOnly Attribute Will Be Executed.



C# Program

Now We Have To Make Sure That The textbox1 Must Contain Some Text (Can't Be Blank). For This We Will Use If Statement To Check Whether textbox1.text Is Blank Or Not
string fname = textBox1.Text;
if  (!(fname == "")) // checking textbox is blank or not
     {
     // Next Code
     }
else
	  {
     MessageBox.Show("Enter File Name","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
In The Above Code , We Have Used fname For textbox1

Lets Check Whether The Entered File Exists Or Not. For This We Have To Import System.IO Namespace.

using System.IO; // Importing Namespace
if (File.Exists(fname))
       {
         // Next Code
       }
     else
       {
          MessageBox.Show("File Not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
Now If The Entered File Exists , Then We Have To Check That File's Attribute That Whether That File Is ReadOnly Or Not. If That File Is Not ReadOnly , Then No Need To Remove The ReadOnly Attribute.
Now Here Is Some Logic. Suppose That File Has More Than One Attribute Like ReadOnly & Hidden Or ReadOnly & Archieve , Then In Case If We Are Checking For Only ReadOnly Condition , Then It Will Return False. So To Solve This Situation , We Have To Convert The Attribute Of That File Into String Then Have To Checker Whether That String Contains The Word "ReadOnly" Or Not.
To Remove ReadOnly Attribute , We Have To Use FileInfo Class & By Using IsReadOnly = false , We Can Remove ReadOnly Attribute.

FileAttributes attr = File.GetAttributes(fname);
                    string thefile = Convert.ToString(attr);
                    if (thefile.Contains("ReadOnly"))
                    {
                        FileInfo myfile = new FileInfo(fname);
                        myfile.IsReadOnly = false; // Removing ReadOnly Attribute
                        MessageBox.Show("ReadOnly Attribute Removed","Success",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("File Is Not ReadOnly So No Need To Remove ReadOnly Attribute");
                    }
C# Removing ReadOnly Attribute


Complete Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Remove_ReadOnly_Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            string fname = textBox1.Text;


            if  (!(fname == "")) // checking textbox is blank or not
            {
               
                if (File.Exists(fname))
                {
                    FileAttributes attr = File.GetAttributes(fname);
                    string thefile = Convert.ToString(attr);
                    if (thefile.Contains("ReadOnly"))
                    {
                        FileInfo myfile = new FileInfo(fname);
                        myfile.IsReadOnly = false;
                        MessageBox.Show("ReadOnly Attribute Removed","Success",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("File Is Not ReadOnly So No Need To Remove ReadOnly Attribute");
                    }
                }
                else
                {
                    MessageBox.Show("File Not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Enter File Name","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }


        }
    }
}

Frequently Asked Question

Why Not To Set File Attribute To Normal

If You Set File Attribute To Normal , Then It Will Remove All The Attribute Of That File If That File Has More Than One Attribute. For Example , If A File Has ReadOnly & Hidden Attribute , Then After Using Normal FileAttribute, Both The File Attribute Will Be Removed (ReadOnly & Hidden). So Instead Of Using Normal Attribute , We Will Use FileInfo Class & Will Use IsReadOnly = false To Remove Only ReadOnly Attribute.

How To Remove Only ReadOnly Attribute In C# Console Application.

You Can Use Same Code & Same Logic Given Above To Remove ReadOnly Attribute Using C# Console Application.