我有一个使用文件流返回msi包的方法。
public FileStream DownloadMsiFileStream()
{
FileStream fs = new FileStream(@"C:\temp\test.msi", FileMode.Create, System.IO.FileAccess.ReadWrite);
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("deviceupdate");
// Retrieve reference to a blob named "KC.AttendanceManager.PrintServiceInstaller.msi".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.msi");
//Retrive the memorystream
blockBlob.DownloadToStream(fs);
return fs;
}
这非常完美,我可以使用web api方法下载文件流,将流写入文件并最终使用有效的msi包。
但现在我想避免在服务器端将文件写入磁盘,因为它会导致并发问题。相反,我尝试将文件流更改为Memorystream,如下所示:
public MemoryStream DownloadMsi()
{
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("deviceupdate");
// Retrieve reference to a blob named "KC.AttendanceManager.PrintServiceInstaller.msi".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.msi");
MemoryStream ms = new MemoryStream();
//Retrive the memorystream
blockBlob.DownloadToStream(ms);
return ms;
}
但是当我试图稍后将流写入文件(只是服务器端以使其工作)时,这样:
MemoryStream ms = DeviceUpdateManager.GetClientUpdateMsi();
FileStream file = new FileStream(@"C:\temp\test2.msi", FileMode.OpenOrCreate);
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);
file.Close();
ms.Close();
结果是无效(空)msi文件。由于System.Text.Encoding.UTF8.GetString(ms.ToArray())
返回一堆,因此内存流不为空。我如何最终得到一个工作的msi?任何帮助appriciated。
答案 0 :(得分:0)
通常解决方案太简单了。这就是我需要检索我的工作msi包的所有内容:
File.WriteAllBytes(@"C:\temp\test2.msi", ms.ToArray());