Code

Deep file and folder/directory copy in .NET

I just ran up against a requirement to do a deep file and folder/directory copy in .NET (C# to be exact). A quick google didn't turn up any code, so I figured I'd post what I came up with. This is a somewhat naive implementation that may run out of steam for very deep folders or 1000s of files, but works great for most things. This implementation takes a source folder and a destination folder and copies the entire structure including files and folders from the source folder to the destination. So without further ado: private void CopyFolder(string folder, string destFolder) {   Directory.CreateDirectory(destFolder);   foreach (string...

posted @ Monday, January 28, 2008 2:24 PM | Feedback (6)

Get the MIME type of a System.Drawing Image

I needed a method to get the mime type of a System.Drawing Image instance that I'd loaded from an upload. All articles I was able to find online suggest some kind of lookup table solution. There's actually another way. Given an Image i, you can map from the i.RawFormat.Guid property to an ImageCodecInfo.FormatID. So it becomes a very simple for loop to get the MIME type of an image or bitmap. This works for any format System.Drawing/GDI+ supports (bmp, png, tiff, jpg/jpeg, etc). Here's the code: public static string GetMimeType(Image i) {     foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders())     {         if (codec.FormatID == i.RawFormat.Guid)             return codec.MimeType;     }     return "image/unknown"; } This won't work for...

posted @ Thursday, January 17, 2008 12:10 PM | Feedback (7)

Recursively find ASP.NET controls by type with generics

Sometimes you want to work with all controls on an ASP.NET page by type rather than id. I finally got fed up with the lack of builtin support, and wrote the following method that returns all controls inside another control (or page) by type: public List<T> FindControls<T>(Control parent) where <T> : Control {     List<T> foundControls = new List<T>();     FindControls<T>(parent, foundControls);     return foundControls; } void FindControls<T>(Control parent, List<T> foundControls) where <T> : Control {     foreach (Control c in parent.Controls)     {         if (c is <T>)             foundControls.Add((<T>)c);         else if (c.Controls.Count > 0)             FindControls<T>(parent, foundControls);                     } }

posted @ Wednesday, January 02, 2008 4:37 PM | Feedback (6)

Reflector

Lutz Roeder's Reflector is one of the most useful tools out there. It's a disassembler that allows you to point at any assembly and break down the code. Scott Hansleman shows off a new PowerShell language plugin for Reflector as well as a nice snapshot of the other addins. It's a great read -- I keep forgetting about all the useful plugins you can add to Reflector.

posted @ Friday, May 18, 2007 11:21 AM | Feedback (0)

The comprehensive guide to Dispose, Finalization, and Resource Management

I've been developing some disposable objects, and was doing research about the proper patterns to follow. This article is a very detailed dissertation on all aspects of IDispose, Finalizers, etc in .NET. Very useful. http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae

posted @ Tuesday, February 06, 2007 2:15 PM | Feedback (0)

Bitmap to Png converter

As I develop, I often have images that are bitmaps or other formats that I want to convert to transparent .png's for use in a website. I whipped up a little app that does a bmp2png conversion, using the RegistryHelper class to hook the right click context menu. Converting an image in .NET is very straightforward, as long as you are doing a conversion from and to a .NET supported image format. Just load the image using the System.Drawing classes, and then save it as the image format you want. Making a color transparent is easy as well -- call...

posted @ Wednesday, December 20, 2006 11:25 AM | Feedback (4)

Shell File Context Menu Handler

For several programs, I've wanted to be able to add a shell context menu item for a file that runs an exe. After some research, I came up with the following information. This menu is controlled by certain registry entries under the HKCR (Classes Root) entry. In HKCR, there are two relevant entries for each file extension: the actual extension, and the handler for that extension. For .bmp, for example, the extension key is as follows: The key part of the extension key is the default string value that points to the handler for this extension, in this case Paint.Picture. Here's the...

posted @ Wednesday, December 06, 2006 1:06 PM | Feedback (2)

How to dynamically create a generic type

Today, I ran up against a situation where I needed to create a generic type dymnamically, e.g. to specify what type the object was built around on the fly. I spent a while trying several different methods (Reflection.Emit, Lightweight Code Generation (LCG), etc.), but it turns out I was dramatically over-engineering the problem. In .NET 2.0, there is a new Type.MakeGenericType method that returns a new type that accepts a paramarray of types and returns a generic type built from those parameters. Here is the basic usage, using the generic List<T> class for an example: Type genericParameterType = typeof(string); Type genericType = typeof(List<>).MakeGenericType(genericParameterType); ConstructorInfo...

posted @ Tuesday, May 02, 2006 11:18 AM | Feedback (0)

YME now playing plugin for WLM

Update: Just released a small tweak to support the latest versions of WLM. I've been using Yahoo! Music Unlimited for almost a year now. It's a great service. Cheap ($4.99/mo), fairly good selection, and 192kbs quality. The only problem is, it doesn't integrate with MSN Messenger/Windows Live Messenger's now playing display. There is a third-party plugin that sort of provides this functionality, but it's old, hasn't been updated, and doesn't work properly with the latest versions of WLM. So I pulled together some information and code from a few different sources and whipped up my own plugin, using C# and VS 2005....

posted @ Monday, April 10, 2006 6:56 PM | Feedback (27)