c# - 如何将文件从特定文件夹复制到 C# 中的共享文件夹?

标签 c# winforms windows-identity

我正在尝试将现有文件从特定文件夹复制到共享文件夹。所以这是代码:

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);
                    }
                }

            }

我得到这个错误:

System.Security.Principal.IdentityNotMappedException: 'Some or all identity references could not be translated.'

在这一行:

 sec.AddAccessRule(accRule);

我做错了什么?如果您需要更多数据,请告诉我...

编辑:

此外,最终目标是这实际上应该将文件保存到 LAN 网络中特定计算机上的共享文件夹中,但我目前正在尝试将其保存在程序运行的同一台计算机上的共享文件夹中。

编辑 2:

所以我尝试了@PaulKaram 的建议,但我仍然遇到下一个错误:

enter image description here

从图中可以看出我最初保存图像的文件夹在Documents中。这没有问题。当我尝试将其复制到桌面上的特定共享文件夹时,上述错误(访问被拒绝)出现在已在文档中创建的文件夹中。

最佳答案

错误 Some or all identity references could not be translated 意味着找不到您正在使用的身份/帐户。深入观察,我们可以看到 您对这一行有疑问:

FileSystemAccessRule accRule = new FileSystemAccessRule(originalDocumentFolderPath, FileSystemRights.FullControl, AccessControlType.Allow);

看看 FileSystemAccessRule您正在使用的构造函数。这是签名:

public FileSystemAccessRule (string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AccessControlType type);

应该发送的第一个参数是身份,从文档中获取:

The name of a user account.

我不确定你在 originalDocumentFolderPath 中发送了什么。
假设 username 持有您正在模拟的身份,该行应更改为:

FileSystemAccessRule accRule = new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Allow);

您应该注意的另外两件事:

首先,您正在使用网络上的共享文件夹,因此您需要修复此行:

string sharedFolderPath = "\\" + Path.Combine(Environment.MachineName, "Users");

进入这个:

string sharedFolderPath = "\\\\" + Path.Combine(Environment.MachineName, "Users");

当您使用网络文件夹时,您需要在开头使用双反斜杠,并且由于在 C# 中反斜杠转义字符,因此您需要将其写为 \\\\

其次,您还应该注意,您正在尝试复制文件并将文件夹名称作为目标。要解决此问题,您应该在合并共享文件夹路径的末尾添加:

sharedFolderPath = Path.Combine(sharedFolderPath, "file.extension");

最后,这是您应该按预期工作的完整代码:

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(username, 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");
            sharedFolderPath = Path.Combine(sharedFolderPath, "file.extension");
            System.IO.File.Copy(originalDocumentFolderPath, sharedFolderPath);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

关于c# - 如何将文件从特定文件夹复制到 C# 中的共享文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53053219/

相关文章:

c# - 调用 ReadPixels 以从系统帧缓冲区读取像素,而不是在绘图帧内。 UnityEngine.Texture2D :ReadPixels

c# - C# 中的播放列表 (axWindowsMediaPlayer)

vb.net - VB.NET错误-使用结果时,在运行时可能会出现Null引用异常

winforms - DataGridView 中的总计行

javascript - 如何处理连续触发的事件?

c# - 如何在不知道密码的情况下获取另一个用户的 WindowsIdentity?

.net - 所有者和用户 (WindowsIdentity) 之间有什么区别?

c# - 检查 BalloonTooltip 是否被用户关闭

javascript - 使用 Asp.net 和 angularjs 的 Odata

c# - DB ID 需要一个更小的 GUID 替代品,但 URL 仍然是唯一且随机的