计算隔离存储中文件的大小

时间:2010-09-27 22:02:38

标签: silverlight-4.0 isolatedstorage

除了打开文件流并调用“Length”属性之外,我似乎找不到确定隔离存储中文件大小的方法。有没有更有效的方法呢?

由于

2 个答案:

答案 0 :(得分:2)

我发现它有点破解让它发挥作用。您需要做的是使用反射来获取所需文件的完全限定文件路径,然后创建一个新的文件信息对象:

//This is the private field name used for reflection
private const string IsolatedStoreRootDir = "m_RootDir";

//This method takes a file path relative to isolated storage
//and the current store
private static FileInfo GetFileInfo(string path, IsolatedStorageFile store)
{
    return new FileInfo(GetFullyQualifiedFileName(path, store));
}

//This gets the fully qualified path of the root isolated storage directory
//then appends the relative path to it.
private static string GetFullyQualifiedFileName(string path, IsolatedStorageFile store)
{
    return Path.Combine(store.GetType()
      .GetField(IsolatedStorageFileSystem.IsolatedStoreRootDir, 
      System.Reflection.BindingFlags.NonPublic |
      System.Reflection.BindingFlags.Instance).GetValue(store).ToString(), path);
}

//Here's how it's used
static void Main(string[] args)
{
    var store = IsolatedStorageFile.GetUserStoreForAssembly();

    var length = GetFileInfo("TestFile.txt", store).Length;
}

答案 1 :(得分:0)

long Size = 0L;
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open, FileAccess.Read, isoFile))
{
  Size = stream.Length;
}