Category Archives: .NET

Generic Iterator

I just generalized the iterator implemented in my previous blog post to non-recursively iterate any object tree. static IEnumerable<T> Iterate<T>(T rootNode, Func<T, IEnumerable<T>> getChildren) { var stack = new Stack<T>(); stack.Push(rootNode); while (stack.Count > 0) { T node = stack.Pop(); yield return node; IEnumerable<T> childNodes = getChildren(node); foreach (var childNode in childNodes) stack.Push(childNode); } }

Custom DropDown Control

Extensibility is not the strongest feature of Windows Forms. I recently required a custom drop down control like the common color picker or date picker control. I started out with http://www.logresource.com/Question/1080206/showall/ and modified it to properly handle usercontrols and keyboard control. // based on http://www.logresource.com/Question/1080206/showall/ internal class PopupComboBox : ComboBox, IMessageFilter { // http://www.woodmann.com/fravia/sources/WINUSER.H const [...]

VirtualBox Config Workaround

I have been having some trouble with VirtualBox since I upgraded some time ago. Every time I close a VM VirtualBox places an invalid character (for an XML-parser anyway) in the config file, rendering the file unreadable for VirtualBox the next time I want to start it. So I did this naïve Python script to [...]

Read from WebClient with the right encoding

I’ve had some encoding trouble with the DownloadString(Async) method on the WebClient class, so I wrote my own encoding detection to get a string with the correct encoding. readonly static Regex m_enc = new Regex( @”charset=(?’encoding’[^\s]+)”, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase); static string GetString(WebHeaderCollection responseHeaders, byte[] data) { string html; Encoding encoding = null; [...]

Iterate folders non-recursively

Here are a few methods to help iterate complete directory trees and the files in them without using recursive method calls. The directories are read one at a time to keep memory usage at a minimum. These restrictions enables iterating trees in very large file shares. static IEnumerable<string> GetDirectories(string startDirectory) { var dirsToVisit = new Stack<string>(); [...]

Follow

Get every new post delivered to your Inbox.