Today I discovered the best way to access a shared folder across the local network.
This example works outside a domain as well.
I tried to use the web.config directive first, .
But this had too many unwanted effects, when all I wanted to do was upload a file to a shared folder.
So, by using the LogonUser Win32 API I was able to wrap a class around this nicely
22 using (new Impersonator(true, "username", "password", "123.123.123.123"))
23 {
24 if (!Directory.Exists(@"\\123.123.123.123\Foo\bar"))
25 Directory.CreateDirectory(@"\\123.123.123.123\Foo\bar");
26 }
All calls made inside the using clause will then be impersonated.
You might as well use an overloaded constructor to provide the username, password and domain/machine/IP.
Source code: download (Temporarily down)
Cheers!