c# - 如何将文件从特定文件夹复制到 C# 中的共享文件夹?
问题描述
我正在尝试将现有文件从特定文件夹复制到共享文件夹。所以这是代码:
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 中创建的文件夹出现上述错误(拒绝访问)。
解决方案
该错误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);
}
}
}
推荐阅读
- java - 未知实体:org.hibernate.engine.transaction.internal.TransactionImpl
- javascript - 在字符串替换中使用承诺
- android - 如何使选项卡下的选择器滚动到选定的选项卡?
- excel - 将数据复制到一个工作表,其中有四行将每个单元格与原始工作表分开
- javascript - 用 Javascript 发推文的困难
- javascript - 如何在字符串文字中传递字符串数组?
- android - linux中的文件输入输出
- apache-kafka - Kafka:连接到节点 -1 身份验证失败,原因是:SSL 握手失败
- php - Laravel,如何根据此代码修改/编辑 excel?
- javascript - 如何将这两个确切的事件与不同的元素合并?