0

i want copy file by "IREAuthenSharePath" when copy file is change new permission. i want keep old permission.

Code C#

IREAuthenSharePath pAuthen = new IREAuthenSharePath();
string shareNameConfirm = "Test\COPY_File";
string targetPath = pAuthen.LoginToShareMapDrive(serverName, shareNameConfirm, userName, password); // \\10.111.210.20\Test\COPY_File

foreach (GridViewRow row in gvDetail.Rows)
{
  var isChecked = (row.FindControl("cbxStatus") as CheckBox).Checked;
  if (!isChecked) continue;

  string fileName = (row.FindControl("lblName") as Label).Text; //Test01
  string filePath = (row.FindControl("lblHFPath") as Label).Text; // \\10.111.210.20\Test\Ori_File\Test01

  //Copy TAP File
  string sourcePath = @"\\10.111.210.20\Test\Ori_File";
  string sourceFile = Path.Combine(sourcePath, fileName);
  string destFile = Path.Combine(targetPath, fileName);
  File.Copy(sourceFile, destFile, true);
  FileInfo file1 = new FileInfo(filePath);
  FileInfo file2 = new FileInfo(filePath);
  FileSecurity ac1 = file1.GetAccessControl();
  ac1.SetAccessRuleProtection(true, true);
  file2.SetAccessControl(ac1); ==> Error Attempted to perform an unauthorized operation.
}

Example Before Copy File

**File name      Permission**
TEST01         0666
TEST02         0062
TEST03         0444
TEST04         0777

After Copy File

File name      Permission
TEST01         0777
TEST02         0777
TEST03         0777
TEST04         0777

I want the copied file to keep the old permission, I don't want it to have permission 0777.

chrki
  • 6,143
  • 6
  • 35
  • 55
nettoon493
  • 17,733
  • 7
  • 30
  • 45

1 Answers1

0

Can you check if this helps. https://social.msdn.microsoft.com/Forums/en-US/99d3825a-748f-4bd8-a77e-6f4f3b12267b/how-to-give-access-file-full-control-by-c-code-?forum=csharplanguage

FileInfo fileInfo = new FileInfo("database_library.mdb");
        FileSecurity accessControl = fileInfo.GetAccessControl();
        accessControl.AddAccessRule(new FileSystemAccessRule("user account name", FileSystemRights.FullControl, AccessControlType.Allow));
        fileInfo.SetAccessControl(accessControl);