2011-04-03 14 views
0

我在我的Web角色中創建了一個名爲「MyTestCache」的本地存儲,就像我在 ServiceDefinition.csdef文件中那樣。但是當我調用System.IO.File.WriteAllBytes方法時,我得到一個UnauthorizedAccess異常。有誰知道這會造成什麼?當在下面的代碼中創建目錄時,我不會得到這個,只有在編寫時。我正在使用SDK 1.3。爲什麼我在嘗試將文件寫入到Azure中的LocalStorage時收到UnauthorizedAccessException

private void SaveFileToLocalStorage(byte[] remoteFile, string filePath) 
    { 
     try 
     { 
      LocalResource myIO = RoleEnvironment.GetLocalResource("MyTestCache"); 

      // Creates directory if it doesn't exist (ie the first time) 
      if (!Directory.Exists(myIO.RootPath + "/thumbnails")) 
      { 
       Directory.CreateDirectory(myIO.RootPath + "/thumbnails"); 
      } 

      string PathToFile = Path.Combine(myIO.RootPath + "/thumbnails", filePath); 
      var path = filePath.Split(Char.Parse("/")); 

      // Creates the directory for the content item (GUID)    
      if (!Directory.Exists(Path.Combine(myIO.RootPath + "/thumbnails", path[0]))) 
      { 
       Directory.CreateDirectory(Path.Combine(myIO.RootPath + "/thumbnails", path[0])); 
      } 

      // Writes the file to local storage. 
      File.WriteAllBytes(PathToFile, remoteFile); 
     } 
     catch (Exception ex) 
     { 
      // do some exception handling 
      return; 
     } 

    } 
+0

就像旁邊一樣,你可以用多個參數調用'Path.Combine' - 所以'Path.Combine(myIO.RootPath,「thumbnails」,path [0])' - 你可以得到一個文件的目錄路徑使用'Path.GetDirectoryName()' – Stuart 2011-04-04 06:36:14

回答

2

檢查ACL。在SDK 1.3中,默認情況下,Web角色在完整的IIS工作進程中啓動,並使用網絡服務作爲應用程序池的標識。確保網絡服務帳戶有權執行您期望的操作。在你的情況下,你正在嘗試創建一個子目錄,所以很可能你至少需要寫入權限。如果您的角色也修改此目錄上的ACL,則需要授予對此目錄的完全訪問權限。

+0

seva在sdk 1.3中是正確的默認web角色是在完整的IIS工作進程中啓動的。檢查你的權限。你需要授予目錄的完全訪問權限。 – Abhi 2011-04-04 04:08:08

相關問題