Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 6, 2015 10:29 AM
Hello,
I am using C# Windows to copy some image files from my PC to another PC(which is server) using File.Copy method.
This is my code.
******************
string sourcePath = @"D:\UserPhotos\;
string targetPath = @"\192.168.1.100\SharedImage\; //SharedImage is set to Write Permission.
string photoID;
photoID = userList.FirstOrDefault().UserID.ToString();
File.Copy(SourcePath + photoID, targetPath + photoID);
******************
But when I run the code and it entered to File.Copy method and it shows the following error.
"The User Name or Password Is Incorrect."
I don't want to use FTP. Please help.
Best Regards,
Nyi Nyi Aung
All replies (7)
Thursday, August 6, 2015 11:06 AM ✅Answered
Hello,
You can impersonate
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity(username, password);
WindowsImpersonationContext context = idnt.Impersonate();
File.Copy(@"\\192.100.0.2\temp", @"D:\WorkDir\TempDir\test.txt", true);
context.Undo();
Refer:Copy files in a shared location in C# and Copy a file to shared network location with C#.
Please mark as answer or vote as helpful if my reply does
Thursday, August 6, 2015 11:40 AM ✅Answered
Access to shares is only allowed to User that have a password. And then of course you have to give the password. Don't ask me why, the Windows Team decided that.
Even setting the Share and File permissions to "any, full access" won't allow you to get on without password.
Thursday, August 6, 2015 10:59 AM
Make sure your paths are correct, maybe you have a double \ in there somewhere. To build a path do not use the + to concatenate, instead use System.IO.Path.Combine() and pass in your string parts, it will correctly generate a file path.
Writing and reading files requires permissions, this is regardless of the location (local, UNC, an attached device, etc) and the context on which your application is running probably does not have permissions to write to this location or read from the source location. Try this.
- Can you copy a file using windows explorer to this location (try a copy/paste)?
- Can you read a file from the source location using windows explorer (open it).
If both of those pass then try again by running the application directly. If this fails try running the compiled console.exe application using a right click and 'run as administrator', does the error still appear?
Mark as answer or vote as helpful if you find it useful | Igor
Thursday, August 6, 2015 11:37 AM | 1 vote
Hello Rachit Sikroria,
I tried to Impersonate. But the following errors occurs in the following line.
System.Security.Principal.WindowsIdentity idnt = new System.Security.Principal.WindowsIdentity(username, password);
***the name provided is not properly formed account name***
string username = "serveradmin";
string password = "abcd1234";
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
System.Security.Principal.WindowsIdentity idnt = new System.Security.Principal.WindowsIdentity(username, password);
System.Security.Principal.WindowsImpersonationContext context = idnt.Impersonate();
File.Copy(Path.Combine(SourcePath, photoID), Path.Combine(targetPath, photoID));
Thursday, August 6, 2015 11:39 AM
Hello Sparco,
I can read/ write on server folder via client windows explorer. But I still can't copy from client PC.
But I don't want my client using 'run as administrator'.
Thanks,
Nyi Nyi Aung
Thursday, August 6, 2015 12:04 PM
When setting the username please don't forget to set the domain as well as I take it the computers are joined to a windows domain.
I would also try to hard code the file source and destination to ensure that a simple mistake like a wrong slash or missing extension is causing the problem and once you get it working change it back.
Mark as answer or vote as helpful if you find it useful | Igor
Saturday, April 7, 2018 5:27 AM
Hello ,
What would be the username and password for
WindowsIdentity idnt = new WindowsIdentity(username, password);
thanks
madan