Set File Attributes in C#

The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

Use the FileInfo class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files. FileSystemInfo.Attributes property is used to gets or sets the attributes for the current file or directory.

_file.Attributes = System.IO.FileAttributes.Hidden;

The following C# program shows how to set readonly and hidden property to a file .

Full Source C#
using System; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { FileInfo _file = new FileInfo("c:\\test1.txt"); _file.Attributes = System.IO.FileAttributes.ReadOnly; _file.Attributes = System.IO.FileAttributes.Hidden; } catch (System.IO.FileNotFoundException ex) { MessageBox.Show(ex.StackTrace); } } } }