【问题标题】:How to copy file from a specific folder to a shared folder in C#?如何将文件从特定文件夹复制到 C# 中的共享文件夹?
【发布时间】:2018-10-29 20:19:23
【问题描述】:

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

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: '部分或全部 无法翻译身份参考。'

在这一行:

 sec.AddAccessRule(accRule);

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

编辑:

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

编辑 2:

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

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

【问题讨论】:

  • 那么你冒充的身份,是你所期望的吗?为什么要冒充当前用户,反正你不是当前用户吗?
  • @nvoigt 嗯,整个想法是,将文件保存在 LAN 中的计算机上,而不是当前的计算机上。我是当前用户,是的。实际上我对 C# 还很陌生,所以......也许我在这里遗漏了很多东西。
  • 假设启动您的程序的用户可以写入网络上的共享文件夹,如果他们只是使用 Windows 资源管理器,那么您不需要任何东西,只需要最后一行 File.Copy。你能添加更多关于你为什么需要这个的细节吗?如果您选择最简单的选项,即调用 File.Copy,会出现什么问题?
  • 我仍然看不出为什么一个简单的File.Copy 或者Directory.Create 不应该做这项工作。不要误会我的意思,也许它没有,但你需要提供它为什么没有的信息。因为快速浏览一下,冒充自己之类的事情并没有多大意义。因此,您应该回到简单的解决方案,并通过描述您在该轨道上遇到的问题来推进工作。
  • @nvoigt 好的,我会尽量做到这一点,然后带着我晚上得到的东西回来。

标签: c# winforms windows-identity


【解决方案1】:

错误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);

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

用户帐户的名称。

我不确定您在 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);
        }
    }
}

【讨论】:

  • 感谢您的回答,对于我迟到的回复感到抱歉。我尝试了您的解决方案,但出现了一些错误。你能看看我的编辑(2)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
相关资源
最近更新 更多