开发者

Calculate the size of a file in Isolated Storage

I can't seem to find a way to determine the size of a file in Isolated Storage besides opening up the file stream and calling the "Length" 开发者_开发百科property. Is there a more efficient way of doing this?

Thanks


I found a bit of a hack to make it work. What you have to do is use reflection to get the fully qualified file path to the file you want then create a new file info object:

//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;
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜