Click here to Skip to main content
14,927,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a c# code to copy image to specific folder just as simple as below:
string fileName = "image.jpg";
string source = "c:\photos\" + fileName;
string destination = "\\\photos\" + fileName;
File.Copy(source, destination);

Problem is: destination is shared folder on Server computer in the LAN and it requires authentication (username, password). I do not want it for my users. "Windows 2008" runs on my server.

Is there any way to do authentication to access shared folders of Server in code?

you can use this small C# Class for impersonating a User[^]
and copy file as below
using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
   // code that executes under the new context.
   File.Copy(source, destination);

Additional reference:
Connect to a UNC Path with Credentials[^]
orgilhp 10-Jun-14 6:42am
Is DomainName "\\\photos\" for my case?
orgilhp 10-Jun-14 7:22am
how about if my server doesn't belong to any domain, what the domainname should be?
Pls show me a sample of DomainName.
DomainName is "\\\photos\" for my case, right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900