计算独立存储中文件的大小

发布于 2024-09-24 20:03:30 字数 78 浏览 3 评论 0原文

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2024-10-01 20:03:30

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

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

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;
}
听你说爱我 2024-10-01 20:03:30
long Size = 0L;
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open, FileAccess.Read, isoFile))
{
  Size = stream.Length;
}
long Size = 0L;
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open, FileAccess.Read, isoFile))
{
  Size = stream.Length;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文