I am trying to copy existing file from a specific folder to a shared folder. So this is the code:
if (!System.IO.File.Exists(fullPath))
{
using (WindowsIdentity.GetCurrent().Impersonate())
{
try
{
image.Save(fullPath);
System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(originalDocumentFolderPath);
FileSystemAccessRule accRule = new FileSystemAccessRule(originalDocumentFolderPath, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
string sharedFolderPath = "\\" + Path.Combine(Environment.MachineName, "Users");
sharedFolderPath = Path.Combine(sharedFolderPath, username);
sharedFolderPath = Path.Combine(sharedFolderPath, "Desktop");
sharedFolderPath = Path.Combine(sharedFolderPath, "SharedFolder");
System.IO.File.Copy(originalDocumentFolderPath, sharedFolderPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
And I get this error:
System.Security.Principal.IdentityNotMappedException: 'Some or all identity references could not be translated.'
at this line:
sec.AddAccessRule(accRule);
What am I doing wrong? If you need more data, please let me know...
EDIT:
Also, the final goal is that this should actually save files into a shared folder on a specific computer in LAN network, but I am currently trying to save it in the shared folder, on the same computer, where program runs.
EDIT 2:
So I tried what suggested by @PaulKaram but I still getting the next error:
From the picture, it can be seen the folder in the Documents where I firstly save image. That goes without problems. When i try to copy it on the specific shared folder on a Desktop, the error above (access denied) arises for the folder already created in Documents.