C# file operations includes the changing of attributes of file also. This example will help you to remove the readonly file attribute from a given file. Example shows that the file have read only attribute and we have removed the readonly attribute by masking using & operator.
C# Program:
using System;
using System.IO;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
string txt_file = @"c:\Demo_File.txt";
File.SetAttributes(txt_file,FileAttributes.ReadOnly);
bool is_ReadOnly = ((File.GetAttributes(txt_file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
Console.WriteLine("ReadOnly Attribute After Add: " + is_ReadOnly.ToString());
// delete/clear ReadOnly attribute
File.SetAttributes(txt_file, File.GetAttributes(txt_file) & ~FileAttributes.ReadOnly);
is_ReadOnly = ((File.GetAttributes(txt_file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
Console.WriteLine("ReadOnly Attribute After Remove: " + is_ReadOnly.ToString());
Console.ReadLine();
}
}
}
0 comments:
Post a Comment