使用c#,我正在从用户在手机上输入的url下载文件。当它将文件写入IsolatedStorage时,它将向该文件写入太多字节,因此用于打开这些文件的程序将不会打开。WP7 IsolatedFileStorageStream - 错误的字节大小
当我调试时,位大小为451,258字节,但是当从IsolatedStorage中导出文件时,它是454,656字节。它用剩余空间填充空间。无论如何调整这个文件的大小?修剪最后的额外空间并保存?
原谅我的无知,因为我是C#和WP7开发新手。我真的很感谢帮助。
这里是我的代码:
public void readCompleteCallback(Object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
try
{
//string fileName = txtUrl.Text.Substring(txtUrl.Text.LastIndexOf("/") + 1).Trim();
string fileName = "DownloadedNZB.nzb";
bool isSpaceAvailable = IsSpaceIsAvailable(e.Result.Length);
if (isSpaceAvailable)
{
// Save mp3 to Isolated Storage
using (var isfs = new IsolatedStorageFileStream(fileName,
FileMode.CreateNew,
IsolatedStorageFile.GetUserStoreForApplication()))
{
long fileLen = e.Result.Length;
byte[] b = new byte[fileLen];
e.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, b.Length);
isfs.Flush();
isfs.Close();
MessageBox.Show("File downloaded successfully");
}
}
else
{
MessageBox.Show("Not enough to save space available to download the file");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show(e.Error.Message);
}
}
我敢打赌,你的意思是文件的大小,而不是位大小。据我所知,所有位的大小是一样的......并且这也适用于字节大小... –
它似乎将它填充到下一个块大小......只是真的需要一种修剪该文件 - 认为这将解决它。但我不知道。谢谢 – SJamG
这个错误发生在Read()调用中,你忽略它的返回值。您必须循环读取所有字节,直到Read()返回0. –