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);
}
}
}
}
Related Topics
- How to write a text file in c#
- Append text to an existing file in C#
- How to use C# Directory Class
- How to use C# File Class
- How to use C# FileStream Class
- How to use C# Textreader Class
- A simple C# Text Reader source code
- How to use C# TextWriter Class
- How to use C# BinaryWriter Class
- How to use C# BinaryReader Class
- How to convert XPS file to Bitmap
- How to C# Path Class
- How to create a pdf file in C#
- How to create PDF file from Text file
- Write Database data to pdf file
Related Topics