Q:

C# program to set attributes of specified file

0

Given a file, and we have to set its attributes using C# program.

File.SetAttributes()

This is a method of "File class, which is used to define (set) new file attributes.

Syntax:

void SetAttributes (path, FileAttributes);

Parameter(s):

  1. path - Filename with its location.
  2. FileAttributes - This object is used to set attributes of file.

File attributes can be following:

  • Archive
  • Compressed
  • Device
  • Hidden
  • ReadOnly, etc
  •  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            
            FileAttributes F1 = File.GetAttributes("B123.TXT");
            Console.WriteLine("Attributes before Method Call are :"+F1.ToString());

            File.SetAttributes("B123.TXT", FileAttributes.ReadOnly);

            FileAttributes F2 = File.GetAttributes("B123.TXT");
            Console.WriteLine("Attributes After Method Call are :" + F2.ToString());

        }
    }
}

Output

Attributes before Method Call are :Hidden, Archive
Attributes After Method Call are :ReadOnly

Note: In above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# file handling (File class) solved programs/examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to get attributes of a specified file... >>
<< C# program to delete a text file...