如何:枚举独立存储的存储区
This page is specific to:.NET Framework Version:
2.03.5
.NET Framework 开发人员指南
如何:枚举独立存储的存储区

更新:2007 年 11 月

您可以使用 IsolatedStorageFile 静态方法 GetEnumerator 枚举当前用户的所有独立存储区。GetEnumerator 获取 IsolatedStorageScope 值并返回 IsolatedStorageFile 枚举数。User 是唯一受支持的 IsolatedStorageScope 值。若要枚举存储区,您必须具有指定 IsolatedStorageContainmentAdministerIsolatedStorageByUserIsolatedStorageFilePermission。当使用 IsolatedStorageScopeUser 进行调用时,GetEnumerator 返回为当前用户定义的 IsolatedStorageFiles 数组。

EnumeratingStores 示例

下面的代码示例获得按用户和程序集隔离的存储区并创建几个文件。调用 GetEnumerator 方法并将结果放入 IEnumerator 中。然后代码依次通过 IEnumerator,添加文件的大小,并将结果报告给控制台。实际枚举发生在私有 EnumerateTheStore 方法中,为了清楚起见,将该方法与代码的其他部分分开,放在文件的底部。

Imports System
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Collections

Public Module modmain

    Sub Main()

        ' Get an isolated store for this assembly and put it into an
        ' IsolatedStorageFile object.

        Dim isoStore As IsolatedStorageFile
        isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)

        ' This code creates a few files so that they can be enumerated.

        Dim FileA As IsolatedStorageFileStream
        FileA = New IsolatedStorageFileStream("TestFileA.Txt", FileMode.Create, isoStore)
        Dim FileB As IsolatedStorageFileStream
        FileB = New IsolatedStorageFileStream("TestFileB.Txt", FileMode.Create, isoStore)
        Dim FileC As IsolatedStorageFileStream
        FileC = New IsolatedStorageFileStream("TestFileC.Txt", FileMode.Create, isoStore)
        Dim FileD As IsolatedStorageFileStream
        FileD = New IsolatedStorageFileStream("TestFileD.Txt", FileMode.Create, isoStore)

        FileA.Close()
        FileB.Close()
        FileC.Close()
        FileD.Close()

        ' There might be a small delay between when the above code
        ' executes and when the files are created in the store. 
        ' Closing and opening the store in this example ensures that
        ' the common language runtime has finished creating the files.

        isoStore.Close()
        isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly, Nothing, Nothing)


        ' This line of code calls a method at the bottom of the program
        ' that puts all the files in isoStore into an IEnumerator.

        Dim allFiles As IEnumerator
        allFiles = EnumerateTheStore(isoStore)

        Dim totalSize As Long

        ' This code counts up the sizes of all the stores.

        While (allFiles.MoveNext())
            Dim store As IsolatedStorageFile

            ' Explicity convert the current object in allFiles
            ' into an IsolatedStorageFile.

            store = CType(allFiles.Current, IsolatedStorageFile)

            ' Find the current size of the IsolatedStorageFile as
            ' an UInt64, and convert that to a string. Get the value
            ' of the string as a double and convert that to a Long.

            totalSize += CLng(Val(store.CurrentSize.ToString))

        End While

        ' Print out the value.

        System.Console.WriteLine("The size of all the isolated stores in the user scope = " + totalSize.ToString)

    End Sub

    Function EnumerateTheStore(ByVal isoStore As IsolatedStorageFile) As IEnumerator

        Return isoStore.GetEnumerator(IsolatedStorageScope.User)

    End Function
End Module


请参见

参考

其他资源

© 2010 Microsoft Corporation 版权所有。   保留所有权利 | 商标 | 隐私权声明
Page view tracker
为轻量型库评级
x
依无脚本原则生成的轻量型库 (loband),添加了大家要求的功能:搜索框和默认代码语言选择。
您喜欢这个搜索框吗?
您喜欢标签式代码块吗?
此主题有用吗?
提供详细反馈。
谢谢
x
感谢您帮助改善 MSDN Online。
反馈意见
切换视图
经典视图
轻量型视图
无脚本视图
切换视图