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 Time in which data Written to file system.io namespace c# (.net 2.0).

Author: Zunnair
Download Source Code : 847_io.zip

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

1)       GetLastWriteTime()

2)       SetLastWriteTime()

Get Last Write Time And Date Of a File:

You can get file last write date and time in which data written to file by using static function GetLastWriteTime() 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.

GetLastWriteTime()  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)

                {

                    File.WriteAllText(str_Path,"dddd"); 

                    DateTime obj = File.GetLastWriteTime(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

                File.WriteAllText(str_Path, "dddd")

                Dim obj As DateTime = File.GetLastWriteTime(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 write time and date of a file.

Set Last Write Date And Time of a File:

1)       File class also provides us a facility to set last write date and time of a file. SetLastWriteTime() is a static function of a file class which sets last write 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.SetLastWriteTime(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.SetLastWriteTime(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 write 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 file last time in which data written to file

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Get and Set file Last Access Time and Date using System.io in c# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net