从天蓝色存储下载文件时,我遇到访问路径被拒绝的异常,下面是代码片段。
string path = @"c:\TEMP";
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["CloudStorageString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Retrieve reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("167_PostP45_temp.png");
// Save blob contents to a file.
using (var fileStream = System.IO.File.OpenWrite(path))
{
blockBlob.DownloadToStream(fileStream);
}
您正在根目录下创建一个文件C:
called TEMP
没有扩展名。的根C:
是受保护的位置,非管理程序无法写入。
另一件可能给你带来这个异常的事情是如果你尝试调用File.OpenWrite
在目录上,我认为这更有可能发生在您身上。
如果您尝试保存到文件夹C:\TEMP
您必须将文件名添加到您要打开的路径中。
using (var fileStream = File.OpenWrite(Path.Combine(path, "167_PostP45_temp.png"))
{
blockBlob.DownloadToStream(fileStream);
}
这也假设文件夹C:\TEMP
已经存在。如果您想使用用户的实际临时文件夹,请使用Path.GetTempPath()
为拿到它,为实现它
using (var fileStream = File.OpenWrite(Path.Combine(Path.GetTempPath(), "167_PostP45_temp.png")))
{
blockBlob.DownloadToStream(fileStream);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)