Easily Create a Timestamped Filename in .NET

Eric says crazy filename parsing is something else you should stop doing.  If you’re creating output files, you probably need to timestamp the file name to keep the files separate.  Step away from the keyboard before you do anything crazy–you can do this in one line of VB.NET:




Dim _filename As String = String.Format(“MyFile.{0}.xml”, Now.ToString(“yyyyMMddHHmm”))


 

Stop Using DatePart for Formatting Dates!

If you’re still formatting dates as such:

lblDate.text = datepart(“d”,Now) & “/” & datepartt(“m”,now) &
“/” & datepart(“yyyy”,now)

then stop this instant!  Step away from the keyboard and no one gets
hurt.

There are better ways in ASP.NET to format dates.  You could replace the
above line of code with:

lblDate.text = getdate().tostring(“MM/dd/yyyy”)

and end up with exactly what you needed.  ASP.NET is full of great
format strings.  Remmeber the old FormatCurrency(MyCash) command?  You
can replace that with MyCash.ToString(“C”).  Download the .NET 2.0 SDK and
look up “formatting strings”; you’ll find all these goodies in
there.

cache-control: no store to prevent page review

In my article on Preventing Page Review After Logout With Forms Authentication, I talked about several HTTP headers that can be used to direct browsers not to cache pages locally.  In one comment, a reader said they had used the article’s code, but Firefox was still caching pages.  Another reader left a comment about using the “cache-control: no-store” header to prevent Firefox from caching pages.  If you see that Firefox is caching secured pages, try adding this header to your pages.  If possible, add it to your master page or page template.


ASP.NET, you can set this header by using the HttpCachePolicy.SetNoStore method.  Put this in your page_load at the latest.  You can also set this in your page’s HEAD section  by adding the following line of code:


<META HTTP-EQUIV=”CACHE-CONTROL” CONTENT=”NO-CACHE”>


In the IIS control panel, you can set headers to be automatically added to every response.  This is discussed briefly at http://support.microsoft.com/kb/815313/, but if you’re in a shared host environment, you probably don’t have access to the IIS control panel.


The “Cache-Control: No Store” header can cause problems with PDF files in IE 6.  Microsoft has a KB article on this at http://support.microsoft.com/default.aspx?scid=kb;en-us;812935.  File downloads via SSL may also beimpacted if you use this header; see http://support.microsoft.com/?kbid=323308 for more details.  This second article involves a registry edit.


Also, remember that browsers need to cache image files if you’re using image rollovers, so be careful where you use any of these headers.  You might mess up your menu.