Рассчитать размер файла в изолированном хранилище - PullRequest
2 голосов
/ 28 сентября 2010

Кажется, я не могу найти способ определить размер файла в изолированном хранилище, кроме открытия потока файлов и вызова свойства "Длина". Есть ли более эффективный способ сделать это?

Спасибо

Ответы [ 2 ]

2 голосов
/ 28 сентября 2010

Я нашел немного хака, чтобы заставить его работать. Вам нужно использовать отражение, чтобы получить полный путь к нужному файлу, а затем создать новый объект информации о файле:

//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;
}
0 голосов
/ 29 августа 2014
long Size = 0L;
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open, FileAccess.Read, isoFile))
{
  Size = stream.Length;
}
...