Click here to Skip to main content
15,884,983 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:
C#
string fileName = "image.jpg";
string source = "c:\photos\" + fileName;
string destination = "\\172.16.242.41\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?
Posted

you can use this small C# Class for impersonating a User[^]
and copy file as below
C#
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[^]
 
Share this answer
 
v2
Comments
orgilhp 10-Jun-14 6:42am    
Is DomainName "\\172.16.242.41\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 "\\172.16.242.41\photos\" for my case, right?
 
Share this answer
 

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