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