File Set, Get and Remove Attributes in C#






4.15/5 (9 votes)
File set, get, remove attributes in C#
Introduction
Many times, we need to change the attributes of file and folder while manipulating file operations in C# so that this article provides some explanation of how to set, get and remove attributes of file.
Using the Code
Create the sample console application and after that, add System.IO
namespace to access and manipulate the file operation.
//Add System.IO namespace
using System.IO
Create any text file in any directory and right click on that file and see the properties of file as read-only, hidden as given in the following image:
Now you can see that the file is not read-only and not hidden in the above image. To set read-only or hidden attributes of this text file, I write the following code to set these attributes.
using System;
using System.IO;
namespace FileAttributes
{
public class Program
{
static void Main(string[] args)
{
//Get file text file path
string filePath = @"C:\temp\TestFile.txt";
// Create the file if it does not exist.
if (!File.Exists(filePath))
{
File.Create(filePath);
}
//SetAttributes function for to set read-Only attributes of file
//The file is a candidate for backup or removal.
File.SetAttributes(filePath, System.IO.FileAttributes.ReadOnly);
Console.WriteLine("The File attributes have been set successfully.");
Console.ReadLine();
}
}
}
Now again, right click on the text file and see the file properties. We can see that read-only attribute will be set of text file.
We can set other attributes as follows:
//To backup or removal file.
File.SetAttributes(filePath, System.IO.FileAttributes.Archive);
//To compressed file.
File.SetAttributes(filePath, System.IO.FileAttributes.Compressed);
//To reserved for future use file.
File.SetAttributes(filePath, System.IO.FileAttributes.Device);
//To set the file is a directory.
File.SetAttributes(filePath, System.IO.FileAttributes.Directory);
//To make data encrypted.
File.SetAttributes(filePath, System.IO.FileAttributes.Encrypted);
//To hide file
File.SetAttributes(filePath, System.IO.FileAttributes.Hidden);
//The file or directory includes data integrity support.
//When this value is applied to a file, all data streams in the file
//have integrity support.When this value is applied to a directory,
//all new files and subdirectories within that directory, by default, include integrity support.
File.SetAttributes(filePath, System.IO.FileAttributes.IntegrityStream);
//To set file has no special attributes
File.SetAttributes(filePath, System.IO.FileAttributes.Normal);
//The file or directory is excluded from the data integrity scan.
//When this value is applied to a directory, by default,
//all new files and subdirectories within that directory are excluded from data integrity.
File.SetAttributes(filePath, System.IO.FileAttributes.NoScrubData);
//The file will not be indexed by the operating system's content indexing service.
File.SetAttributes(filePath, System.IO.FileAttributes.NotContentIndexed);
// To set file offline.The data of the file is not immediately available.
File.SetAttributes(filePath, System.IO.FileAttributes.Offline);
//To set read only attributes
File.SetAttributes(filePath, System.IO.FileAttributes.ReadOnly);
//To set reparse point attributes
//The file contains a reparse point,
//which is a block of user - defined data associated with a file or a directory.
File.SetAttributes(filePath, System.IO.FileAttributes.ReparsePoint);
//To set Sparse File attributes
//The file contains a reparse point,
//which is a block of user - defined data associated with a file or a directory.
File.SetAttributes(filePath, System.IO.FileAttributes.SparseFile);
//To set file part of operating system or is used by the operating system.
File.SetAttributes(filePath, System.IO.FileAttributes.System);
//To file make temporary
File.SetAttributes(filePath, System.IO.FileAttributes.Temporary);
To get the attributes of a file, we use GetAttributes(filepath)
function to get file attributes.
File.SetAttributes(filePath, System.IO.FileAttributes.Hidden);
System.IO.FileAttributes attributes = File.GetAttributes(filePath);
Console.WriteLine(string.Format( "File attribute: {0}", attributes));
Console.ReadLine();
Here is the output of the above code:
Let us see how to remove attributes of files. First upon set hidden attributes to hide text file and see the directory where the file is placed, text file will be hidden.
//First upon set hidden attributes to text file
File.SetAttributes(filePath, System.IO.FileAttributes.Hidden);
To remove hidden attributes:
File.SetAttributes(filePath, System.IO.FileAttributes.Normal);
Actually, it removes all the attributes. We need to write more code to remove single attributes of a file.
using System;
using System.IO;
namespace FileAttributesConsole
{
public class Program
{
static void Main(string[] args)
{
string filePath = @"C:\temp\TestFile.txt";
if (!File.Exists(filePath))
{
File.Create(filePath);
}
FileAttributes attributes = File.GetAttributes(filePath);
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
// Show the file.
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
File.SetAttributes(filePath, attributes);
Console.WriteLine("The {0} file is no longer hidden.", filePath);
}
else
{
// Hide the file.
File.SetAttributes(filePath, File.GetAttributes(filePath) | FileAttributes.Hidden);
Console.WriteLine("The {0} file is now hidden.", filePath);
}
Console.ReadLine();
}
private static FileAttributes RemoveAttribute
(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}
}
Points of Interest
File attributes is most important to know how to set, get, removed applied attributes. Mostly file operation is used in many applications and we need to know and understand all basics of file operation such as open, read - write, set attributes, get attributes, copy, move file, etc. Using this tip, we are able to change and modify the attributes of a file, also at least know to set and get attributes of a file. We can set and get all types of attributes that are available in C#.
Have fun!
History
- 30th August, 2016 - Initial post