Wednesday, 15 December 2010

How to Append Attributes to a File using C#

The below C# code will help to append the new attributes to a file without deleting the existing attributes. for example the below code add the hidden attribute to file Demo_File.txt.

How to Append Attributes to a File using C#

Source Code:

using System;
using System.IO;

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

            bool is_Hidden = ((File.GetAttributes(txt_file) & FileAttributes.Hidden) == FileAttributes.Hidden);
            Console.WriteLine("Hidden Attribute Before Append: " + is_Hidden.ToString());

            File.SetAttributes(txt_file, File.GetAttributes(txt_file) | FileAttributes.Hidden);

            is_Hidden = ((File.GetAttributes(txt_file) & FileAttributes.Hidden) == FileAttributes.Hidden);
            Console.WriteLine("Hidden Attribute After Append: " + is_Hidden.ToString());           

            Console.ReadLine();
        }    
    }
}

No comments:

Post a Comment