C# set File Attributes Read Only [change file to read only mode]

/ / 0 Comments

C# Set File Attributes Read Only: While working on a c# console application, I have a requirement to make all the files in read-only mode. In short, I need to change the file attribute and make them all read-only to end user. In C# with File.SetAttributes() we can easily set the attribute of any given file. Before writing any code first, we need to import System.IO namespace into our application. In C# we handle files with System.IO, it allows us to read/write any given file. We can also manipulate directories on the file system with System.IO. Here in this article will learn how to set file attributes in C#. You can also check my previous article how to remove the read-only attribute from the file in c#.

File.SetAttributes(): This method is used to sets the specified FileAttributes to the given file.
Syntax:
public static void SetAttributes(
string path,
FileAttributes fileAttributes
)
path: the path of the file. fileAttributes: this parameter is used to set attribute/ property of the file. Below we have listed the fileAttributes member, which we can set to the file or directory.
C# Code: Here is the bellow written code we set the read-only attribute to the given file.

using System;
using System.IO;

namespace CONSOLE_TUT
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"D:DummyTextFile.txt";
            File.SetAttributes(filePath, FileAttributes.ReadOnly);

            Console.WriteLine("The {0} file is now readonly.", filePath);
            Console.ReadLine();
        }
    }
}
Output:

Before: 

After :  

   List of fileAttributes members:

  • Archive: The file is a candidate for backup or removal.
  • Compressed: The file is compressed.
  • Device: Reserved for future use.
  • Directory: The file is a directory.
  • Encrypted: The file or directory is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is the default for newly created files and directories.
  • Hidden: The file is hidden and thus is not included in an ordinary directory listing.
  • IntegrityStream: 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.
  • Normal: The file is a standard file that has no special attributes. This attribute is valid only if it is used alone.
  • Offline: The file is offline. The data of the file is not immediately available.
  • ReadOnly: The file is read-only.
  • SparseFile: The file is a sparse file. Sparse files are typically large files whose data consists of mostly zeros.
  • System: The file is a system file. That is, the file is part of the operating system or is used exclusively by the operating system.
  • Temporary: The file is temporary. A temporary file contains data that is needed while an application is executing but is not needed after the application is finished.

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

0 Comments