2005-10-23
My C# and .NET articles
This post contains links to all the (small) articles and pieces of code in C# that I posted. You can always access it via a link on my sidebar. Each time I post something in this category, I'll update this post.
General
- Static destructor in C#
- Rounding a floating point value to a certain precision
- Fast .NET formatting of values
- Reading from and writing to a Stream
- Years, months and days between 2 dates
- Filter numbers out of a string
- Format a file size
- Reading data from an embedded resource
- Reading an image from a file without locking it
- FileCreationWatcher
- if-read-do-while-read + part 2
Windows Forms
- Painting a Control onto a Graphics object
- Copying an HTML-fragment to the Clipboard
- Screen capture tool
- Getting a list of all open windows
- TC Animation Library
- Making your password TextBox more secure
ASP.NET
Mobile development
2005-10-14
Reading from and writing to a Stream
Here you can find a class I wrote that allows you to easily read and write byte arrays from and to a Stream. I created it because I needed such functionality on a NetworkStream (where you can't get the length of the stream).
using System; using System.IO; namespace TC.Utils { public class StreamHelper { private static byte[] ReadByteArray(Stream stream, int length) { byte[] buffer = new byte[length]; int offset = 0; while (offset < length) offset += stream.Read(buffer, offset, length - offset); return buffer; } private static void WriteInt32(Stream stream, int value) { stream.Write(BitConverter.GetBytes(value), 0, 4); } private static int ReadInt32(Stream stream) { return BitConverter.ToInt32(ReadByteArray(stream, 4), 0); } public static void WriteByteArray(Stream stream, byte[] data) { WriteInt32(stream, data.Length); if (data.Length > 0) stream.Write(data, 0, data.Length); } public static byte[] ReadByteArray(Stream stream) { return ReadByteArray(stream, ReadInt32(stream)); } } }To write:
Stream stream = ...;
byte[] data = ...;
StreamHelper.WriteByteArray(stream, data);
To read:
Stream stream = ...;
byte[] data = StreamHelper.ReadByteArray(stream);
EDIT 15 Oct 2005: Martin Plante of Xceed Software has given me some tips to make the code simpler (using BitConverter instead of manually bit-shifting) and more robust. Thanks!
2005-10-08
Napoleon Dynamite
I've just watched Napoleon Dynamite for the 5th time in less than 2 weeks.
The first time I watched it, I was a bit confused. I liked it, but I didn't know why. The movie has a slow pace, and the jokes are not obvious. Then I watched it again. And I realized that I like it because, although the movie takes place in 2004, it takes me back to the 80's, to my youth. It's about the small things in life. The main characters are people we all recognize, average people, anti-heroes.
This movie is a beautiful, feel-good movie that doesn't need sex, violence or prophanities to appeal to people. If you have a confused feeling after watching it once, please watch it again: you might get it, you might pick up subtle things that weren't obvious the first time.
The first time I watched it, I was a bit confused. I liked it, but I didn't know why. The movie has a slow pace, and the jokes are not obvious. Then I watched it again. And I realized that I like it because, although the movie takes place in 2004, it takes me back to the 80's, to my youth. It's about the small things in life. The main characters are people we all recognize, average people, anti-heroes.
This movie is a beautiful, feel-good movie that doesn't need sex, violence or prophanities to appeal to people. If you have a confused feeling after watching it once, please watch it again: you might get it, you might pick up subtle things that weren't obvious the first time.