Sunday, May 1, 2011

Confuserion - a quiz

It can be a little tricky to work out who you are when you’re some running code (‘behind’) in ASP.NET.

Assuming my machine is called MYMACHINE, my domain is MYDOMAIN, my username is rik and I’m logged in as MYDOMAIN/rik, what will the following return?
  1. Environment.UserDomainName & "\\" & Environment.UserName
  2. System.Security.Principal.WindowsIdentity.GetCurrent().Name()
  3. HttpContext.Current.User.Identity.Name
  4. Threading.Thread.CurrentPrincipal.Identity.Name

Bonus point: Whose registry setting will this affect?

Registry.CurrentUser.OpenSubKey
(
  @"Software\Microsoft\Internet Explorer\PageSetup",
  true
).SetValue("footer", "xxx")

Compare KeyValuePair

KeyValuePair doesn't implement IComparable. If it did, I think it would probably work like this:

public static int CompareKeyValuePair<KeyT, ValueT>
(
  KeyValuePair<KeyT, ValueT> a,
  KeyValuePair<KeyT, ValueT> b
)
  where KeyT : IComparable
  where ValueT : IComparable
{
  int keyCompareResult = a.Key.CompareTo(b.Key);
  
  if (keyCompareResult == 0)
    return a.Value.CompareTo(b.Value);
  else
    return keyCompareResult;
}

Copy file content to clipboard

I wrote a tiny application which makes it easier to copy the text from a file to the clipboard. It's a shell extension which adds a new entry to the context menu in Explorer. To install it, just run it once. In Vista or W7, you'll have to use 'Run as Administrator' to make it register. Now you'll have a new menu item in Explorer's context menu, which looks like this:



Released under the MIT licence. Based on a sample application written by Ralph Arvesen.

Find it here.

Context switch timing

I measured context switch overhead on my machines.

It's about 7us on my PIII 600 (Celeron) (Coppermine), memory bandwidth of ~ 2.5 G/s, Linux 2.2.18 (SuSE 7.1)

It's about 0.85us on my Dual Athlon 1800+, Linux 2.4.18 (SuSE 7.3)

It's about 24us(!) on one core of my Opteron 2423 HE

I'm not sure I'm measuring correctly.

Thursday, April 17, 2008

Columns to CSV with LINQ

In SQL, I often use the following idiom as a quick-and-dirty method of turning column values into a single - comma separated - value.


DECLARE @csv NVARCHAR(4000)
SELECT @csv = COALESCE(@csv + ', ', '') + CONVERT(NVARCHAR, ColumnName) FROM TableName

I'm now using LINQ more than raw SQL, so here's my version of the equivalent. If you can improve it, please let me know.


string s = null;
var csv = from i in TableName select s = (s == null ? "" : s + ", ") + i;