This is and neat class I've put together that allows you to specify username and password (and optionally a domain) and act under that identity. Primarily to access a shared folder, but you could easily find more areas of use.
1: using (new Impersonator(true, "username", "password", "123.123.123.123"))
2: {
3: // The new idenity is only active inside this using clause
4: if (!Directory.Exists(@"\\123.123.123.123\Foo\bar"))
5: Directory.CreateDirectory(@"\\123.123.123.123\Foo\bar");
6: }
7:
It makes use of the Win32 API "LogonUser".
Download the source code and give it a try!
Impersonator.7z (3.60 kb)