简体   繁体   English

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

[英]How to copy file from a specific folder to a shared folder in C#?

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.' System.Security.Principal.IdentityNotMappedException:'某些或所有标识引用无法转换。

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

EDIT 2: 编辑2:

So I tried what suggested by @PaulKaram but I still getting the next error: 所以我尝试了@PaulKaram的建议,但仍然遇到下一个错误:

在此处输入图片说明

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. 当我尝试将其复制到桌面上的特定共享文件夹上时,对于已经在文档中创建的文件夹,会出现上述错误(访问被拒绝)。

The error Some or all identity references could not be translated means that the identity/account you're using is not found. 错误Some or all identity references could not be translated意味着找不到您正在使用的身份/帐户。 Taking a deeper look, we can see that you have a problem with this line: 更深入地了解,我们可以看到您对这条线有疑问:

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

Take a look at the FileSystemAccessRule constructor that you're using. 看一下您正在使用的FileSystemAccessRule构造函数。 Here's the signature: 这是签名:

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

The first argument that should be sent is the identity, as taken from the documentation: 应该发送的第一个参数是标识,如文档中所述:

The name of a user account. 用户帐户的名称。

I am not sure what you're sending there in originalDocumentFolderPath . 我不确定您在originalDocumentFolderPath发送了什么。
Assuming username hold the identity you're impersonating, that line should be changed to: 假设username持有您要模拟的身份,则该行应更改为:

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

Two other things that you should be aware of: 您应该注意的另外两件事:

First of all, you're working with a shared folder on a network, therefore you need to fix this line: 首先,您正在使用网络上的共享文件夹,因此您需要解决以下问题:

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

into this: 到这个:

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

When you're working with a network folder, you need double back slash at the start, and since in C# a back slash escapes characters, you'll need to write it as \\\\\\\\ . 当使用网络文件夹时,开始时需要双反斜杠,并且由于在C#反斜杠转义了字符,因此您需要将其写为\\\\\\\\

Second, you should also note, that you're trying to copy the file and giving it the folder name as destination. 其次,还应注意,您正在尝试复制文件并将其文件夹名称指定为目的地。 To fix this you should add at the end of combining the path of the shared folder this: 要解决此问题,您应该在合并共享文件夹的路径末尾添加以下内容:

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

At the end, here's your full code that should work as intended: 最后,这是您的完整代码,应该可以正常工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM