Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to GET and SET properties of any file using system.io namespace in c# (.net 2.0).

Author: Zunnair
Download Source Code : 842_io.zip

In this simple article you will learn how to get and set properties of a file using FILE class in C# .net 2.0.

 File class provide us a static functions GetAttributes() and SetAttributes().These are static functions so they can be called by class name.

Get Attributes Of a File:

You can get file attributes by using static function GetAttributes() of File class.

GetAttributes() takes file path as a parameter and returns FillAttributes class object.

To demonstrate make a window application. Drag one text box and one button on the form. Write file path (like c:\\abc.txt) in the textbox and press button.

Now write the following code on button click event:

C#

private void btn_Get_Click(object sender, EventArgs e)

        {

            str_Path = txtPath.Text;

            if (str_Path != null)

            {

                if (File.Exists(str_Path) == true)

                {

                    FileAttributes obj=File.GetAttributes(str_Path);

                    MessageBox.Show(obj.ToString());

                }

                else

                {

                    MessageBox.Show("FILE DOES NOT EXIST");

                }

            }

            else

            {

                MessageBox.Show("please write path");

            }

        }

VB

Private Sub btn_Get_Click(ByVal sender As Object, ByVal e As EventArgs)

        str_Path = txtPath.Text

        If str_Path IsNot Nothing Then

            If File.Exists(str_Path) = True Then

                Dim obj As FileAttributes = File.GetAttributes(str_Path)

                MessageBox.Show(obj.ToString())

            Else

                MessageBox.Show("FILE DOES NOT EXIST")

            End If

        Else

            MessageBox.Show("please write path")

        End If

    End Sub

This is a simple code get attributes of a file.

Set Attributes of a File:

File class also provides us a facility to set attributes of a file. SetAttributes() is a static function of a file class which sets attributes.

It takes file path and File Attribute as a parameter. It returns nothing.

To demonstrate make a window application. Drag one text box and one button on the form. Write file path (like c:\\abc.txt) in the textbox and press button.

Now write the following code on button click event:

C#

private void btn_Set_Click(object sender, EventArgs e)

        {

            str_Path = txtPath.Text;

            if (str_Path != null)

            {

                if (File.Exists(str_Path) == true)

                {

                    File.SetAttributes(str_Path, FileAttributes.ReadOnly);

                   // Deifferent attributes

                    //File.SetAttributes(str_Path, FileAttributes.Hidden);

                    //File.SetAttributes(str_Path, FileAttributes.Compressed);

                    MessageBox.Show("ATTRIBUTE SET");

                }

                else

                {

                    MessageBox.Show("FILE DOES NOT EXIST");

                }

            }

            else

            {

                MessageBox.Show("please write path");

            }

        }

VB

Private Sub btn_Set_Click(ByVal sender As Object, ByVal e As EventArgs)

        str_Path = txtPath.Text

        If str_Path IsNot Nothing Then

            If File.Exists(str_Path) = True Then

                File.SetAttributes(str_Path, FileAttributes.[ReadOnly])

                ' Deifferent attributes

                'File.SetAttributes(str_Path, FileAttributes.Hidden);

                'File.SetAttributes(str_Path, FileAttributes.Compressed);

                MessageBox.Show("ATTRIBUTE SET")

            Else

                MessageBox.Show("FILE DOES NOT EXIST")

            End If

        Else

            MessageBox.Show("please write path")

        End If

    End Sub

This simple code set attributes of a file.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

 

            this.Text = "Devasp.net FILE HANDLING Application";

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

 

        Me.Text = "Devasp.net FILE HANDLING Application"

 End Sub

 

This simple article tells how to get and set different properties of files.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to replace file with another file and DELETE file using system.io namespace in c# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net