2012-07-24 108 views
0

我正在使用azure blob storage。我存储在存储一个txt文件,现在我想通过读它:流式读取器异常URI格式不受支持

StreamReader reader = new StreamReader(Blob.Uri.AbsoluteUri); 
Blob containes the reference of the blob where txt file is stored. 

但是在这里我得到异常(URI formats are not supported.)。我认为streamreader不支持URI合成。任何人都可以为此给我一个更好的工作吗?

(我知道一个我们可以利用的CloudBlobDowmloadToFile方法来下载文件,然后使用创建使用Server.Mappath一个地址,并把该地址,而不是URI。但如果这个那么任何其他更好的解决方案,请让我知道)

回答

3

看看WebClient.OpenReadWebClient.DownloadString方法。

你的代码将看起来像这样的事情:

var wc = new WebClient(); 
using(var sourceStream = wc.OpenRead(Blob.Uri.AbsoluteUri)){ 
    using(var reader = new StreamReader(sourceStream)){ 
     // Process 
    } 
} 

或者,如果该文件是小(文本文件通常很小),:

var wc = new WebClient(); 
var fileContent = wc.DownloadString(Blob.Uri.AbsoluteUri);