Wednesday, April 20, 2016

Tip # 1: Avoid race condition in app variables

Application.Lock();
Application["tod"] = HttpUtility.HtmlEncode(tod.Text);
Application.UnLock();

using (System.IO.StreamWriter file = new System.IO.StreamWriter(Server.MapPath("~/App_Data/tod.txt"), false))
{
     file.WriteLine(tod.Text);
}


Tip # 2: Writing file data in single line of code in simplest way

System.IO.File.WriteAllText(Server.MapPath("~/App_Data/tod.txt"), tod.Text);


Tip # 3: For pages that are not to be cached always use this meta tag

<meta http-equiv="cache-control" content="no-cache" />
-Works with Chrome

<meta http-equiv="Pragma" content="no-cache" />
-Does NOT work with chrome but works with FF and IE


Tip # 4: Reading file data using single line of code in simplest way

 HttpUtility.HtmlEncode( System.IO.File.ReadAllText(Server.MapPath("~/App_Data/tod.txt")


instead of

//string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
        // System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
    }

No comments: