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 Last Access Time and Date using System.io in c# (.net 2.0).

Author: Zunnair
Download Source Code : 846_io.zip

In this simple article you will learn how to get and set last access time and date of file using FILE class in C# .net 2.0.

You can get file last access date and time by using static function GetLastAccessTime() of File class. This is a static function so it can be called by class name. You do not need to create an object, to access this function.

GetLastAccessTime()  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.GetLastAccessTime(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.GetLastAccessTime(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 to get last access time and date of a file.

Set Last Access Date And Time of a File:

File class also provides us a facility to set last access date and time of a file. SetLastAccessTime() is a static function of a file class which sets last access date and time of file. This is a static function so it can be called by class name. You do not need to create an object, to access this function.

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.SetLastAccessTime(str_Path,DateTime.Today);

                    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.SetLastAccessTime(str_Path, DateTime.Today)

                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 sets last access 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 last access time and date of file.

Article Comments
Hi!
File.SetLastAccessTime function doesn't work for me, what can i do or what might be problerm ?

Posted on 12/25/2009 2:02:58 AM by Ahmet UZUN

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Read multiple text lines from and Write multiple text lines in files using “FILE CLASS” in c# (.net 2.0)

Disclaimer - Privacy
© 2002-2017 DevASP.net