【问题标题】:How copy file keep old permission?复制文件如何保留旧权限?
【发布时间】:2017-04-24 19:59:01
【问题描述】:

当复制文件更改新权限时,我想通过“IREAuthenSharePath”复制文件。我想保留旧权限。

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.
}

示例 复制文件之前

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

复制文件后

File name      Permission
TEST01         0777
TEST02         0777
TEST03         0777
TEST04         0777

我希望复制的文件保留旧权限,我不希望它有权限 0777。

【问题讨论】:

标签: c# permissions copy access-control accesscontrolexception


【解决方案1】:

你能检查一下这是否有帮助。 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);

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 2012-04-17
    • 1970-01-01
    • 2013-11-16
    • 2013-10-08
    • 2011-11-02
    • 2016-04-21
    • 2011-07-26
    • 2011-10-13
    相关资源
    最近更新 更多