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 file creation date and time using “FILE CLASS” in c# (.net 2.0)

Author: Zunnair
Download Source Code : 838_io.zip

In this simple article you will learn how to get and set creation date and time of a file using FILE class

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

Get Creation Time Of a File:

You can get file creation date and time by using static function GetCreationTime() of File class.

GetCreationTime() takes file path as a parameter and returns DateTime 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_GetDate_Click(object sender, EventArgs e)

        {

            str_Path = txtPath.Text;

            if (str_Path != null)

            {

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

                {

                    DateTime obj = File.GetCreationTime(str_Path);

                    MessageBox.Show(obj.ToString());

                }

                else

                {

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

                }

            }

            else

            {

                MessageBox.Show("please write path");

            }

        }

VB

Private Sub btn_GetDate_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 DateTime = File.GetCreationTime(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 creation time and date of a file.

Set Creation Time of a File:

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

It takes file path and Date Time 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_SetDate_Click(object sender, EventArgs e)

        {

           

            str_Path = txtPath.Text;           

            if (str_Path != null)

            {

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

                {                                

                    File.SetCreationTime(str_Path,DateTime.Now);

                    MessageBox.Show("DATE SET");

                }

                else

                {

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

                }

            }

            else

            {

                MessageBox.Show("please write path");

            }

        }     

VB

Private Sub btn_SetDate_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.SetCreationTime(str_Path, DateTime.Now)

                MessageBox.Show("DATE SET")

            Else

                MessageBox.Show("FILE DOES NOT EXIST")

            End If

        Else

            MessageBox.Show("please write path")

        End If

    End Sub

This simple code set creation date and time 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 creation time of a file using FILE class

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Copy file from one location to another with different name (1 overloaded function) using “FILE CLASS” using SYSTEM.IO in .net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net