Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

How to save files in server folder using windows application with c#?



Thanks and Regards,
Murali.K
Posted

Hi,

You can save any file to sever if the server path is shared.

E.G.

C#
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

WindowsIdentity identity = new WindowsIdentity(username, password);

WindowsImpersonationContext context = identity.Impersonate();

try
{
    File.Copy(@"c:\temp\File.txt", @"\\server Name or IP\folder\File.txt", true);
}
catch
{
    context.Undo();
}
 
Share this answer
 
Comments
Member 10292068 4-Dec-15 5:03am    
i have a confusion is the Username and password is of Server PC or local PC ?
Suvabrata Roy 4-Dec-15 8:07am    
You are going to copy file from your local machine and paste it into server so you required server credentials which have access to that particular folder
yagyesh pathak 24-Dec-22 10:18am    
If server computer windows password disable then
Suvabrata Roy 5-May-23 1:20am    
Then you need to use AD authentication :
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "username", "password"))

https://learn.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.principalcontext.-ctor?view=netframework-4.8#System_DirectoryServices_AccountManagement_PrincipalContext__ctor_System_DirectoryServices_AccountManagement_ContextType_System_String_System_String_System_String_

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