October 2007 Entries
Here is a typical select filter string that you might perform on a DataTable:

clients.Select(string.Format(" DisplayName = '{0}' ", ClientName));

However if ClientName in the above example contains an apostrophe the select will fail. A simple fix is to the use following code to escape it:

ClientName.Replace("'","''")

Which replaces a single apostrophe with two.

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
Here's how to create a Domain Local Group in Active Directory using C#
using (DirectoryEntry myDE =
    new DirectoryEntry(LDAPPath, LDAPUsername, LDAPPassword))
{
   DirectoryEntries myEntries = myDE.Children;
   using (DirectoryEntry group = myEntries.Add("CN=" + Name, "group"))
   {
      group.Properties["sAMAccountName].Add(Name);
      group.Properties["groupType].Add("-2147483644"); //DOMAIN LOCAL GROUP
      group.CommitChanges();
   }
}
Also one thing to note - if the Name ends in a full stop it will fail with a really non-helpful message of "A device attached to the system is not functioning."

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
I've been using the Microsoft Excel COM object in C# to allow me to open spreadsheets, process them and then upload to a web service, all worked nicely - or so i thought! After a while i noticed the PC running slowly - so i checked Task Manager and was horrified to see hundreds of EXCEL.exe processes running!

As the Excel Application object is a COM object there's no nice Dispose() method to call, and setting to null didn't help either... after some searching i found the answer:
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//Do some stuff here...
excel.Quit();
while (Marshal.ReleaseComObject(excel) != 0) { }
excel = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Some of the posts i found didn't have the last two lines which call the .NET garbage collector - and it didn't solve the problem until i added them. Also some people found they had to call Marshal.ReleaseComObject() on any Workbook or Worksheet objects they'd used (in my case i didn't need to).

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
If you've ever got the following annoying message when trying to RDP to a remote server:

"The terminal server has exceeded the maximum number of allowed connections. The system cannot log you on. Please try again or consult your system administrator"

Then normally you've got to either ask another user to log off (not easy if its a remote server) or wait for them to. A little known trick is to use the following command line parameter when launching remote desktop: /console

From the command prompt type:

mstsc /console

This will launch it in console mode, which in layman's terms means you'll be connecting to the server as if you were sat right in front of it, not a terminal server session which is the default (its worth noting that bar a registry hack XP doesn't allow virtual sessions - you always connect to the console - which is why if you remote desktop to an XP machine it will log off any other users)

If you find this useful it might be worth changing the shortcut in your start menu to include the parameter /console, otherwise you'll need to remember to launch RDP from the command line each time.

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
The new service pack (SP1) for Microsoft Virtual Server 2005 R2 added some new functions to the COM API. There's too many to list, but the one i've found most useful allows you to return the guest machine's computer name as it appears on the network - very useful for automation scripts, or simply launching a Remote Desktop client (as i wanted).

Unfortunately the documentation on it isn't great and it took me a while to find it when using C#, (thanks mainly to a few emails to/from Ben aka "Virtual PC Guy" http://blogs.msdn.com/virtual_pc_guy). It turns out you need to cast the GuestOS property of the virtual machine to IVMGuestOS which allows you access to the .ComputerName property:

string name = ((IVMGuestOS2)Machine.GuestOS).ComputerName;
I then took the above to launch an RDP session

using (Process p = new Process())
{
    p.StartInfo.Arguments = string.Format(@"/v:{0} /console", name);
    p.StartInfo.FileName = Settings.Default.RDPPath;
    p.Start();
}

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist