peter peter - 3 months ago 23
ASP.NET (C#) Question

How to execute a method one time in ASP.net

I have web form which has a button.When you click that button,it will create a text file and write something to it.Just imagine like i am writing large things of 1G content ,which will change once in a day.And this is an asp.net application and many users will use.So suppose first user clicks at morning 6.o clock it will generate .Now i want to resuse it for others rather creating a new one till next morning 6 o clock.How to do it.I am posting a small prototype code

try
{
File.WriteAllText("E:\\test.txt", "welcome");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}


NB:This is an asp.net application so cant think of thread.So i am not thinking

While(true)
{
Thread.Sleep() etc
}

Answer

Use File.GetLastWriteTime Method to check last modification in file

try
{
   if(!File.Exists("E:\\test.txt") )
   {
     File.WriteAllText("E:\\test.txt", "welcome");     
   } 
   else
   { 
       if(File.GetLastWriteTime(path).Day != DateTime.Now.Day)   
       {
         //code for next day
       }  
   }

}
catch (Exception ex)
{
  Response.Write(ex.Message);
}