我正在努力在控制台应用程序中访问之前获得文件夹的权限C#
文件夹的路径,文件夹的路径可能是ftp的本地路径。同样在我的机器上工作,但是当部署在生产服务器上时,它开始抛出错误System.InvalidOperationException: Method failed with unexpected error code 64
。
代码接收的确切错误是
System.InvalidOperationException: Method failed with unexpected error code 64.
at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
at System.Security.AccessControl.FileSystemSecurity..ctor(Boolean isContainer, String name, AccessControlSections includeSections, Boolean isDirectory)
at System.Security.AccessControl.DirectorySecurity..ctor(String name, AccessControlSections includeSections)
at UploadData.FolderManager.IsConfiguredFolderAccessible(String path, Folder folder)
抛出错误的完整代码是
private static void IsConfiguredFolderAccessible(string path, Folder folder)
{
// If the file can be opened for exclusive access it means that the file
// is no longer locked by another process.
try
{
if (!Directory.Exists(path))
{
LogHelper.Log(string.Format("Folder does not exist on given path {0}. Please re-create folder, grant permission and re-start the UPC utility. ", folder.Path), LogHelper.LogLevel.Error);
MailComponent.SendMail(string.Format("Folder does not exist on given path {0}. Please re-create folder, grant permission and re-start the UPC utility.", folder.Path), "Folder does not exist");
return;
}
else
{
var accessControlList = Directory.GetAccessControl(path);
if (accessControlList == null)
{
LogHelper.Log(string.Format("AccessControlList on Folder {0} are not defined", folder.ToString()), LogHelper.LogLevel.Error);
MailComponent.SendMail(folder.ToString(), "AccessControlList on Folder are not defined");
}
var accessRules = accessControlList.GetAccessRules(true, true,
typeof(System.Security.Principal.SecurityIdentifier));
if (accessRules == null)
{
LogHelper.Log(string.Format("AccessRules on Folder {0} are not defined", folder.ToString()), LogHelper.LogLevel.Error);
MailComponent.SendMail(folder.ToString(), "AccessRules on Folder are not defined");
}
foreach (FileSystemAccessRule rule in accessRules)
{
if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write)
continue;
if (rule.AccessControlType == AccessControlType.Deny)
{
LogHelper.Log(string.Format("Access permission denied on Folder {0}", path), LogHelper.LogLevel.Error);
MailComponent.SendMail(folder.ToString(), string.Format("Access permission denied on Folder {0}", path));
}
}
}
}
catch (PrivilegeNotHeldException pv)
{
LogHelper.Log(string.Format("Access permission denied on Folder {0}, Error detail : {1}", path, pv.ToString()), LogHelper.LogLevel.Error);
MailComponent.SendMail(pv.ToString(), string.Format("Access permission denied on Folder {0}", path));
throw pv;
}
catch (IOException io)
{
LogHelper.Log(string.Format("Folder does not exist on given path {0}, Error detail : {1}", path, io.ToString()), LogHelper.LogLevel.Error);
MailComponent.SendMail(io.ToString(), string.Format("Folder does not exist on given path {0}.Please re-create folder, grant permission and re-start the UPC utility.", path));
throw io;
}
catch (Exception ex)
{
LogHelper.Log(string.Format("General error occured on Folder {0}, Error detail : {1}", path, ex.ToString()), LogHelper.LogLevel.Error);
MailComponent.SendMail(ex.ToString(), "General error occured");
throw ex;
}
}
该错误是由对本机 Windows GetSecurityInfo()
函数的调用生成的。您可以在 NativeObjectSecurity
类的源代码中看到这一点。十六进制中的错误 64 或 0x40 在 winerror.h
中具有以下定义:
ERROR_NETNAME_DELETED
指定的网络名称不再可用。
因此,您的问题很可能与通过网络访问文件夹有关。