Friday 28 May 2010

AddThis validation issue

The AddThis button is a great addition to any website, however if your site needs to conform to W3C standards you may need to tweak the default HTML snippet that is provided, the issue is the use of the # in one of the JavaScript references:


<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=blahblah">
</script>


Simply change the # to a ? and you should be validating fine:


<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js?username=blahblah"></script>

Monday 17 May 2010

Linq equivalent of for-each

If you've ever wondered about using Linq for a more elegant implementation of a for each loop then this may be useful, use the Select<> method to perform actions against a set of items, as long as you return a value of any sort then you can perform any action during the selection.

In the example below I need to remove all folders that were written to over an hour ago, it also returns info on the folders processed and whether they were successfully removed:


if (string.IsNullOrEmpty(RootWorkingFolder) || !Directory.Exists(RootWorkingFolder)) return;

Directory.GetDirectories(RootWorkingFolder)
.Where(f => Directory.GetLastWriteTime(f) < DateTime.Now.AddHours(-1))
.Select(delegate(string f)
{
try
{
Directory.Delete(f, true);
return new {Folder = f, Removed = true};
}
catch (IOException)
{
return new {Folder = f, Removed = false};
}
}).ToList();


The ToList method is to ensure that the query is executed immediately.

Pretty useful to know :-)

Wednesday 5 May 2010

Visual Studio 2010 running slow

For anyone suffering with the performance of Visual Studio 2010, particularly intellisense, Microsoft has published a support article on the matter, apparently an update to Windows Automation API 3.0 can be applied that will improve matters.

The article is here: http://support.microsoft.com/kb/981741/.