2010-01-10 73 views
1

完全重複的設置ACL時:https://stackoverflow.com/posts/2035107未經授權的操作上的遠程文件/目錄

試圖刪除文件,並保存在遠程位置操作。 當作爲一個控制檯應用程序運行時,它工作正常,但是從XP_CMDSHELL(SQL服務器)時調用xp_cmdshell的從

[4804] System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. 

[4804]在System.Security.AccessControl.Win32.GetSecurityInfo運行時 這裏是失敗的例外(ResourceType資源類型,字符串名稱,SafeHandle句柄,AccessControlSections訪問控制段,RawSecurityDescriptor & resultSd) [4804]在System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType資源類型,布爾isContainer,字符串名稱,SafeHandle句柄,AccessControlSections includeSections,布爾createByName, ExceptionFromErrorCode exceptionFromErrorCode,Object exceptionContext) [4804]在System.Security.AccessControl.FileSystemSecurity..ctor(布爾isContainer,字符串名稱,AccessControlSections包含的部分,布爾isDirectory) [4804] at System.Security.AccessControl.DirectorySecurity..ctor(String name,AccessControlSections includeSections) [4804]在System.IO.DirectoryInfo.GetAccessControl(AccessControlSections includeSections) [4804] at Excel.SetAcl(String filename,String account)in D:\ SAABZX01D \ dev \ libraries \ EXCEL \ Class1.cs:line 228 [4804]在Excel.doKEStats(字符串baanId,字符串FROM日期,字符串TODATE)在d:\ SAABZX01D \ dev的\庫\ EXCEL \的Class1.cs:線87

下面是代碼

public static bool SetAcl(string filename,string account) 
    { 
     FileSystemAccessRule rule = new FileSystemAccessRule(account, FileSystemRights.FullControl, AccessControlType.Allow); 
     string path= System.IO.Directory.GetDirectoryRoot(filename); 
     System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(filename); 


     bool what = false; 
     DirectorySecurity security = di.GetAccessControl(AccessControlSections.Access); 
     security.ModifyAccessRule(AccessControlModification.Add, rule, out what); 




     di.SetAccessControl(security); 
     return what; 

    } 

回答

1

的問題DirectoryInfo的帶有完整路徑(包括文件名).. 下面是修改代碼,工程..

public static bool SetAcl(string filename, string account) 
{ 
    FileSystemAccessRule rule = new FileSystemAccessRule(account, FileSystemRights.Write, AccessControlType.Allow); 

    PermissionSet fp = new PermissionSet(PermissionState.Unrestricted); 
    fp.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read, new string[] { filename })); 
    fp.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.PathDiscovery, new string[] { filename })); 
    fp.Assert(); 

    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(filename)); 


    bool what = false; 
    DirectorySecurity security = di.GetAccessControl(); 

    security.ModifyAccessRule(AccessControlModification.Add, rule, out what); 
    di.SetAccessControl(security); 
    return what; 

} 
0

確保運行的帳戶SQL Server具有執行該文件操作的權限。

相關問題