The IsNullOrEmpty method was added to the String class in the 2.0 Framework, and it makes testing for an empty or null string as easy as a single function call. Instead of having to test the String object if it’s null then checking if it’s empty, this can now be done in one easy method call.
VB.NET
Dim s As String
If String.IsNullOrEmpty(s) Then
Console.WriteLine(“String is null or empty.”)
Else
Console.WriteLine(String.Format(“String = {0}”, s))
EndIf
This isn’t exactly an earth shattering discovery, but it’s useful nonetheless.
Cross site scripting can be a tough vulnerability to eliminate, but it doesn’t necessarily have to be. If you’re working on an ASP.NET project, the Microsoft Anti-XSS library is easy to use and freely available. Like a lot of developers, I’ve rolled my own anti-XSS by escaping specific characters, but it’s usually clunky and let’s face it. There are still bound to be vulnerabilities. The MS library can be used to encode HTML, HTML attributes, JavaScript, VBScript, as well as encode for XML and XML attributes.
Always encode data from untrusted inputs. Just a few examples include:
- Databases
- Form fields
- Session variables
- Query string
- Cookies
Using the library is very simple. Just add a reference to the dll to your project, and you’re ready to go. Here’s a quick and dirty code example encoding a value from the query string:
string Name = AntiXss.HtmlEncode(Request.QueryString["Name"]);
A good rule to live by is “When in doubt. Encode it.” Just don’t encode it twice.
You can download the library from Microsoft here.
I recently completed a project at work that included the requirement to monitor data files on 30 or more different Windows NT4 machines for changes. Naturally I thought of using the FileSystemWatcher class in the System.IO namespace. As anyone who has used the FileSystemWatcher has come to realize, it can be unreliable. Specifically, if one of the NT4 machines were rebooted, the watcher would “lose” its ability to capture the file modification events I needed. What was my solution? Well, it’s a bit of a hack. Actually, it’s a really big hack. I used the Timer class in the System.Threading namespace to restart the watchers at a specified interval and to check for changes to the files that I might have missed. The files don’t change very often, so the solution has worked so far. I also can’t install anything on the NT4 machines, so this is what I was forced to do. Come on. I’m not the only one to hack something together like this. Am I?
In part one of my series on scripting iTunes using C# I showed you how to remove dead tracks from your iTunes library. Now let’s take it one step further and get rid of all those duplicate tracks you might have. For my purposes, I determined a duplicate track to be any track that has the same artist, album and track name as another track in the library. When I find a duplicate, I want to keep the track with a higher bitrate. iTunes will show you a list of the duplicates in your library, but I’m much to lazy to sort out what should be deleted and what shouldn’t.
I used a Dictionary object to store the tracks I checked with the key being the track name. If a matching name is found, then I dig a little deeper to verify that a duplicate has actually been found, then I keep the one with a higher bitrate.
Download the VS2008 C# Express project here.
Here’s a code sample:
//create a reference to iTunes
iTunesAppClass iTunes = new iTunesAppClass();
//get a reference to the collection of all tracks
IITTrackCollection tracks = iTunes.LibraryPlaylist.Tracks;
int trackCount = tracks.Count;
Dictionary<string, IITTrack> trackCollection = new Dictionary<string, IITTrack>();
ArrayList tracksToRemove = new ArrayList();
for (int i = trackCount; i > 0; i–)
{
if (tracks[i].Kind == ITTrackKind.ITTrackKindFile)
{
string trackKey = tracks[i].Name + tracks[i].Artist + tracks[i].Album;
if (!trackCollection.ContainsKey(trackKey))
{
trackCollection.Add(trackKey, tracks[i]);
}
else
{
if (trackCollection[trackKey].Album != tracks[i].Album ||
trackCollection[trackKey].Artist != tracks[i].Artist)
{
trackCollection.Add(trackKey, tracks[i]);
}
else if (trackCollection[trackKey].BitRate > tracks[i].BitRate)
{
tracksToRemove.Add(tracks[i]);
}
else
{
tracksToRemove.Add(tracks[i]);
}
}
}
}
//now remove all duplicates
for (int i = 0; i < tracksToRemove.Count; i++)
{
tracksToRemove[i].Delete();
}
Tags: C#, iTunes
If you’re like me you’ve got thousands of songs in your iTunes library. I got a new notebook PC a couple months back, and I decided to move all my music to it since it’s got plenty of disk space. I know I had lots of songs in my library whose files didn’t exist anymore. There’s a script for removing dead tracks included with the iTunes COM download, so I figured I would port it over to a C# project.
The code is pretty straightforward. Get a reference to iTunes, check the file location of each track in the library and delete the tracks whose files don’t exist.
//create a reference to iTunes
iTunesAppClass iTunes = new iTunesAppClass();
//get a reference to the collection of all tracks
IITTrackCollection tracks = iTunes.LibraryPlaylist.Tracks;
for (int i = trackCount; i > 0; i–)
{
IITTrack track = tracks[i];
if (track.Kind == ITTrackKind.ITTrackKindFile)
{
IITFileOrCDTrack fileTrack = (IITFileOrCDTrack)track;
//if the file doesn’t exist, we’ll delete it from iTunes
if (fileTrack.Location == String.Empty || !System.IO.File.Exists(fileTrack.Location))
{
fileTrack.Delete();
}
}
}
You can download the project here (Download Project). I put it into a WinForm, used a worker thread to take care of the iTunes work because it can take some time to check a 10K song library, and output the dead tracks removed to a list box.

I think in part 2 I’ll add the ability to find duplicate songs and remove the copy with the lower bitrate. I know for sure that I’ve got quite a few of them. Any other ideas for what can be done with the iTunes SDK to clean up your music library or to just make life simpler?
Here’s what I used for this project:
Visual C# 2008 Express Edition
iTunes 7.4.2.4
iTunes COM for Windows SDK
FYI. I found about 175 dead tracks out of over 7300 in my library.