Sponsored Ad

Thursday, December 16, 2010

How to Clear/Remove Files Attributes using C#

This is next post of File attributes. This will help you to modify/Clear the file attributes. You need to set file attribute to FileAttributes.Normal, if you want to clear the file attributes. Once you assign normal attribute it will clear all  the attributes assigned to it.

How to Clear/Remove Files Attributes using C#

C# Code to Clear any File Attributes:

using System;
using System.IO;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string my_file = @"c:\myfile.txt";         

            // set archive and read only attributes
            File.SetAttributes(my_file, FileAttributes.Archive |
                                         FileAttributes.ReadOnly);
            bool ReadOnly_attribute = ((File.GetAttributes(my_file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            Console.WriteLine("ReadOnly Attribute: " + ReadOnly_attribute.ToString());

            //clear attributes
            File.SetAttributes(my_file, FileAttributes.Normal);

            ReadOnly_attribute = ((File.GetAttributes(my_file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
            Console.WriteLine("ReadOnly Attribute: " + ReadOnly_attribute.ToString());

            bool Archive_attribute = ((File.GetAttributes(my_file) & FileAttributes.Archive) == FileAttributes.Archive);
            Console.WriteLine("Archive Attribute: " + Archive_attribute.ToString());

            Console.ReadLine();
        }

    }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers