【问题标题】:Calculate the size of a file in Isolated Storage计算独立存储中文件的大小
【发布时间】:2014-08-29 08:53:30
【问题描述】:

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

谢谢

【问题讨论】:

    标签: silverlight-4.0 isolatedstorage


    【解决方案1】:

    我发现了一些让它工作的技巧。您需要做的是使用反射来获取您想要的文件的完全限定文件路径,然后创建一个新的文件信息对象:

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

    【讨论】:

    • 您是否在权限受限的环境中进行了测试?当隔离存储是您的唯一权限时,我认为这不会起作用。
    【解决方案2】:
    long Size = 0L;
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open, FileAccess.Read, isoFile))
    {
      Size = stream.Length;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多