Code Snippets

Handy blocks of code you can use.

Fixing the Desktop Icon Drop Shadow Problem on Windows Server 2008

I develop on a daily basis on a Windows Server 2008 R2 machine. I do that because that's my target deployment environment and it's really helpful to be able to actually run the full product and debug right there on my "workstation." As such, I have the full "desktop experience" enabled - Aero, themes, the whole bit. One problem I noticed was that the drop shadows under the icons on the desktop... they just don't stick around. I set my visual effects settings to "best appearance" and everything looks correct, but if I log out and back in, the...

Posting multipart/form-data Using .NET WebRequest

While making my ImageShack plugin for Windows Live Writer I had to figure out how to make a web request that posts data to an endpoint in "multipart/form-data" format. That's a lot different than URL-encoding everything into a querystring format and sticking that in the body of the POST request. If you're uploading one or more files, you need to format things differently. I looked all over for some built-in function that does this, but it doesn't seem to exist in the .NET base class library. Surprising, but also not surprising. I found a question on StackOverflow that talked...

Putting log4net.config Outside of Application Configuration

I use log4net for logging in various applications, but every time I start a new app I forget this and it never quite comes up in Google for me, so here we go. Most of the examples on the log4net site showing configuration shows it right in the App.config/Web.config file for the application. That's a painful way to go if you have, say, a single log4net.config that you want used in several projects or if you otherwise want to stick log4net.config somewhere else. The magic bit that at least I can't easily find and always forget is: ...

Context Menu Icons with DXCore

A long time ago I posted a little sample showing how to get context menus working with your DXCore plugin. As part of a new plugin I'm working on, I wanted to get an icon to show up in the context menu next to my entry in the menu, something like the icon you see for the Refactor! menu: It's possible, but it's not really documented, so here's what you do. First, make sure you're working with a DevExpress.CodeRush.Menus.IMenuButton. This is the type of thing you can click on and have something happen, as opposed to a DevExpress.CodeRush.Menus.IMenuPopup which is what...

Convert a Relative Path to Absolute Path with jQuery and ASP.NET AJAX

I was messing around with relative paths to files (e.g., "../images/error.gif") and needed to convert them to absolute paths (e.g., "http://server/images/error.gif") on the client but couldn't figure out how. Then I saw this nifty trick to HTML encode things using jQuery and it gave me an idea. String.toAbsolutePath = function(relativePath) { /// <summary> /// Converts a relative file path into an absolute file path. /// </summary> /// <param name="relativePath" type="String"> /// The string with the relative path, like "../foo/bar.gif" /// </param> /// <returns type="String" /> var path = $("<div style=\"background-image:url('" +...

Parsing Currency Values with ASP.NET AJAX

I had to parse a culture-sensitive currency value from a string and couldn't figure out how to do it. I'm using ASP.NET AJAX to do String.localeFormat("{0:c}", value) for writing a currency value to a textbox, but getting it back out... not so easy. The Number.parseLocale extension provided with ASP.NET AJAX is cool for parsing out numbers in a culture-sensitive fashion... if they don't have a currency symbol. So, time to hook that up. Here's what I came out with: Number.parseCurrency = function Number$parseCurrency(str) { /// <summary> /// Parses a string containing a culture-sensitive currency value into a number. ///...

Replace jQuery Validation Message Formatting with ASP.NET AJAX String.format()

I'm working on a site where we're using both ASP.NET AJAX and jQuery to get things done. This includes jQuery Validation for client-side validation functionality. One of the things that comes with jQuery Validation is a $.validator.format method that replaces the {n} style parameters in a string with arguments - basically, a very lightweight String.format. ASP.NET AJAX provides a really nice implementation of String.format that is pretty full-featured and understands format strings. For example, you can do String.format("{0:d}", mydate) to format a date in short date format. Snazzy stuff. Unfortunately, the jQuery Validation one isn't that robust... so I figured I'd marry...

Getting the Windows OS Version in MSBuild

I saw a tweet come across asking how to get the OS version in MSBuild. MSBuild will automatically import any environment variables... but it appears the OS version isn't an environment variable, so it doesn't have any OS version info you can get out of the box. You can, however, do a registry key lookup. Here's a quick MSBuild snippet showing how to get the Windows version out of the registry. If you want more version-related information, there's a lot in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion. <?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="DisplayVersion" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <OsVersion>$(registry:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion@CurrentVersion).$(registry:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion@CurrentBuildNumber)</OsVersion> </PropertyGroup> ...

Robust File Finding in Your App with MSBuild

I'm working on an application where we wanted to be able to provide some config or command line parameters that would specify a particular set of files for processing. What might be considered the "standard" set of .NET framework libraries comes with System.IO.Directory.GetFiles(), but the wildcard support is pretty weak. I don't want to find "*.dll" recursively, I want support like "recursively search folders under this tree for *.dll but exclude Foo.dll" or something. So I started thinking - what has that sort of include/exclude support and robust wildcard matching? MSBuild. When you use the CreateItem task, you can specify all nature of...

Automatically Set Album Artist in iTunes

I'm working on getting my Windows Media Center to play my iTunes files (by installing codecs and tag readers) and one of the things I'm doing is setting the album artist on all of the tracks because that's something Windows Media Center needs set to get album art properly. While doing it, I found there were basically two classes of track that didn't already have the album artist tag set - those that fell into "Various Artists" (like soundtracks and such) and those where the artist should also be set as the album artist. To make it easy and less tedious,...

Fixup Script for DVDFab HD Decrypter Full Disc Rip Folder Structure

This is, admittedly, a bit of a niche thing, but it's helpful for me so I figured someone might be interested. I use DVDFab HD Decrypter to rip movies for my DVD Library and it works really well except when you tell it to rip movies to a particular folder, it generates this little folder structure that makes it difficult to just copy over the movie to my Windows Home Server. Say I tell it to rip to the "C:\Movies\Really Cool Movie" folder - I'll come out with a structure like this: C:\Movies\Really Cool Movie | +-FullDisc ...

Moving From CruiseControl.NET to TeamCity

I admit to loving me some good ol' CruiseControl.NET for continuous integration but there's just something to be desired from the UI and ease-of-use/ease-of-administration standpoint. CI Factory brings it a step closer to what I'm looking for, but after doing some evaluation, we ended up trying out TeamCity. It's awesome. It's easy to set up, easy to administer, flexible, customizable... really, the experience thus far has been pretty great. At work we're testing it out with the free Professional edition, but if it works out on this project, we'll be getting the full Enterprise edition. I know that if I set up...

Find Tracks Missing Artwork in iTunes

I'm a huge stickler for metadata in my iTunes library and for the most part, I can find incorrect or missing data by using the iTunes smart playlists - I have one, for example, that checks for "Year is 0" so I can find tracks that don't have a year associated with them. One of the missing features in there is that you can't set up a smart playlist that tells you which tracks don't have artwork. So, to fill that gap, I wrote a script that does it for you. This little JScript, when run, will create a regular playlist...

Testing Lists with Typemock Isolator

I've seen this question a few times in the Typemock forums so I figured I'd post a little something on it: How do you mock a list using Typemock Isolator? The challenge we currently have is that Isolator can't mock types that are declared in mscorlib, which includes things like List<T>, so if you expose a list in your class, you have some potentially interesting mocking challenges, depending on how you choose to expose it. If you wrap the list in methods/properties on your class, when you set up mocks, you'll be setting up mocks against your class - not the list -...

Creating Resource-Only Assemblies

Working on some localization stuff, I've come across the need for resource assemblies to be created from .resx files in a post-build step - I have the .resx files, but I don't want them at all to be attached to my application. I just want them built into a tiny hierarchy of resource-only assemblies so I can get a ResourceManager on them and have string lookup behave as expected. I also find that sometimes I need to recompile .resx without recompiling the whole app and I forget the command-lines to do all that. The overall process: Convert your .resx...