25th
January
2013
If you are looking for some more .NET bloggers to subscribe to, check out my .NET Programming RSS Feeds. It’s a exported bundle from my Google Reader. I’ve subscribed to these feeds over several years so I am sure many of the blogs are no longer active.
http://www.google.com/reader/bundle/user%2F06213114670216208115%2Fbundle%2FProgramming%20-%20Dotnet
posted in Dotnet/.NET - C#, Programming, Tips |
9th
March
2012
I was looking for the most efficient way to remove excess white space from a string and wrote the following benchmark. Guess which algorithm is faster?
const int iterations = 200000;
const string expr = " Hello world! Why are so many spaces? Testing One two three four five.";
// Remove excess space using Regex
var doRegex = new Action(() =>
{
for (int i = 0; i < iterations; i++)
{
var newStr = Regex.Replace(expr, @"\s{2,}", " ");
}
});
// Remove excess space using Split/Join
var doSplit = new Action(() =>
{
for (int i = 0; i < iterations; i++)
{
var newStr = String.Join(" ", expr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
});
var benchMark = new Func<string, Action, long>((name, a) =>
{
var sw = Stopwatch.StartNew();
a();
sw.Stop();
Console.WriteLine(name + ": " + sw.ElapsedMilliseconds);
return sw.ElapsedMilliseconds;
});
// Warming up
Console.WriteLine("Warming up.");
doRegex();
doSplit();
// Run benchmark
long regexElapsed = benchMark("Regex", doRegex);
long splitElapsed = benchMark("Split", doSplit);
On my PC, the Split method is about 7.5 times faster than Regex.

posted in Dotnet/.NET - C#, Programming |
6th
August
2011
Version 1.2 of Transactional File Manager is now available on CodePlex.
Here’s the release notes:
Release Notes
This is a minor bug-fix/minor refactor release.
Fixes:
- Copy now uses the overwrite parameter correctly.
- Fixes crashing problem on some machines due to use of GetsequentialGuid.
- CreateDirectory now supports rolling back nested directories.
- RollbackFile Rollback() may throw exception when original directory has been deleted.
- Security issue when writing to event logs.
Enhancements:
- Upgrade solution/project files to Visual Studio 2010
- Use NUnit instead of VSTS framework
- Changes to support Visual Studio Express
Thanks to karbuke and dorong for their contributions.
posted in Dotnet/.NET - C#, Programming |
7th
September
2010
From my code snippets, here’s a function that will return the starting day of the week for any date:
/// <summary>
/// Gets the start of the week that contains the specified date.
/// </summary>
/// <param name="date">The date.</param>
/// <param name="weekStartsOn">The day that each week starts on.</param>
/// <returns>The start date of the week.</returns>
public static DateTime GetStartOfWeek(DateTime date, DayOfWeek weekStartsOn)
{
int days = date.DayOfWeek - weekStartsOn;
DateTime startOfWeek = days>=0 ? date.AddDays(-days) : date.AddDays(-7 - days);
return startOfWeek;
}
posted in Dotnet/.NET - C#, Programming |
8th
June
2010
It’s my first open source project! I’ve gone open source with my Transactional File Manager. Check out the CodePlex link here.
Use any file system as a transactional file system! Transactional File Manager is a .NET API that supports including file system operations such as file copy, move, delete in a transaction. It’s an implementation of System.Transaction.IEnlistmentNotification (works with System.Transactions.TransactionScope).

More on Transactional File Manager in my original blog post on it. If you are interested in contributing to the project, let me know.
posted in Dotnet/.NET - C#, Programming, Uncategorized |
10th
May
2010
If you get one of these errors starting MSMQ Server or installing MSMQ on Windows (Server 2008), this article describes a potential solution:
- You cannot start MSMQ Service with the following entry in the Application Event Log: The Message Queuing service cannot start. The internal private queue ‘admin_queue$’ cannot be initialized. If the problem persists, reinstall Message Queuing. Error 0xc00e0001.
- You cannot install MSMQ with the following error: Attempt to install Message Queuing Server failed with error code 0×80070643. Fatal error during installation. The following features were not installed: Message Queuing Services/Message Queuing Server.
For me, the solution was to delete the Registry key KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSMQ, then try the installation again.
See also:
posted in Dotnet/.NET - C#, MSMQ, Programming |
27th
March
2010
Your house is haunted. You can’t stand living with the ghosts anymore and decide to call in the professional, a ghost hunter. The ghost hunter comes, nicely equipped with all kinds of shiny ghost hunting equipment, and proceeds to immediately pass out and fall to to the floor with a loud thud at first sight of the ghosts.
Well, in a nutshell, that is the story of the new Event Viewer in Windows Vista and Windows Server 2008. The new Event Viewer looks very nice and all. Trouble is, it now won’t work when there is a syntax error in machine.config or other .NET Framework problems. Apparently, in Windows Vista and Windows 2008, someone at Microsoft decided to rewrite the Event Viewer utility (a perfectly usable and solid utility in previous versions of Windows) in .NET.
So, the lesson I learned from this is: don’t hire ghost hunters who are afraid of ghosts. Or, don’t write a utility designed to view monitoring and troubleshooting messages that can fail due to unnecessary dependencies. A critical system troubleshooting utility such as Event Viewer should be the last thing that fails.
What to do when you cannot run Event Viewer in Windows Vista/Windows Server 2008/or Windows 7?
If you get the following error:
MMC could not create the snap-in. The snap-in might now have been installed correctly. Name: Event Viewer. CLSID: FX:{b05566ad-fe9c-4363-be05-7a4cbb7cb510}
Try the following:
- Fix any syntax errors in machine.config. Restore to a previously known working copy.
- Rename EventVwr.exe.config in %WINDIR%\System32.
posted in Dotnet/.NET - C#, Programming |
6th
February
2010
To programmatically switch bindings on the fly, you can do it via the constructor of the generated client:
var client = new WeatherClient(“MyEndpoint”);
“MyEndpoint” is the name of the endpoint defined in your config file:
<client>
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="http1" contract="MyContract" name="MyEndpoint" />
</client>
posted in Dotnet/.NET - C#, Programming |
23rd
January
2010
If you get the following exception calling a WebSphere web service from your .NET WCF Client (service reference):
System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. —> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
Try adding this code before the service call:
System.Net.ServicePointManager.Expect100Continue = false;
More info on the 100-Continue behavior from MSDN.
posted in Dotnet/.NET - C#, Java, Programming |
2nd
April
2009
Here’s a method to convert a generic List<T> to a DataTable. This can be used with ObjectDataSource so you get automatic sorting, etc.
/// <summary>
/// Convert a List{T} to a DataTable.
/// </summary>
private DataTable ToDataTable<T>(List<T> items)
{
var tb = new DataTable(typeof (T).Name);
PropertyInfo[] props = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
}
foreach (T item in items)
{
var values = new object[props.Length];
for (int i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
}22/
return tb;
}
/// <summary>
/// Determine of specified type is nullable
/// </summary>
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}
/// <summary>
/// Return underlying type if type is Nullable otherwise return the type
/// </summary>
public static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
}
-
1/22/2010 – Fix to support Nullable types.
posted in Dotnet/.NET - C#, Programming |
Recent Comments