2012-01-15 60 views
0

我想列出对istostorage子目录中的所有文件如何获得所有文件isostorage

Public Function GetAllFilesInDirectory(ByVal DirectoryName As String) As List(Of String) 
     Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication() 

     Dim L As New List(Of String) 
     For Each di As String In isoStore.GetDirectoryNames 
      If di = DirectoryName Or di & "/" = DirectoryName Then 

       For Each fi As String In isoStore.GetFileNames'<-- fails because not the subdirectory is listed 
        If fi.StartsWith(DirectoryName) Then L.Add(fi) 
       Next 
      End If 
     Next 

     Return L 
End Function 

回答

1

我不是一个VB.NET开发的子目录,所以我用一个转换器从我的转换old C# version。看看这有助于阅读所有目录:

Public Shared Sub GetIsolatedStorageView(pattern As String, storeFile As IsolatedStorageFile) 

    Dim root As String = System.IO.Path.GetDirectoryName(pattern) 

    If root <> "" Then 
     root += "/" 
    End If 

    Dim directories As String() = storeFile.GetDirectoryNames(pattern) 
    'if the root directory has no FOLDERS, then the GetFiles() method won't be called. 
    'the line below calls the GetFiles() method in this event so files are displayed 
    'even if there are no folders 
    If directories.Length = 0 Then 
     GetFiles(root, "*", storeFile) 
    End If 


    For i As Integer = 0 To directories.Length - 1 
     Dim dir As String = directories(i) + "/" 



     'Write to output window 
     Debug.WriteLine(root + directories(i)) 


     'Get all the files from this directory 
     GetFiles(root + directories(i), pattern, storeFile) 

     'Continue to get the next directory 
      GetIsolatedStorageView(root + dir + "*", storeFile) 
    Next 


End Sub 

Private Shared Sub GetFiles(dir As String, pattern As String, storeFile As IsolatedStorageFile) 

    Dim fileString As String = System.IO.Path.GetFileName(pattern) 

    Dim files As String() = storeFile.GetFileNames(pattern)  


    Try 
     For i As Integer = 0 To storeFile.GetFileNames(dir + "/" + fileString).Length - 1  
      'Files are prefixed with "--" 
      Debug.WriteLine("--" + dir + "/" + storeFile.GetFileNames(dir + "/" + fileString)(i)) 
     Next 

    Catch ise As IsolatedStorageException 
     Debug.WriteLine("An IsolatedStorageException exception has occurred: " + ise.InnerException) 

    Catch e As Exception 

     Debug.WriteLine("An exception has occurred: " + e.InnerException) 
    End Try 


End Sub 

如果你想为开发目的,你可以使用wp7explorer tool代替。

+0

嗨keyboardP。首先,请允许我表示感谢您的快速回复。你说我需要使用'GetFileName()'的模式来获取子目录文件。 Dim file As String()= storeFile.GetFileNames(pattern)'。首先,我写了文件(“database/test.txt”),然后检查了'.Exists = True',现在我仍然无法使用GetFileNames方法(GetFileNames(“datbase /”))列出文件。 WP7文件浏览器显示该文件,有什么建议? – Nasenbaer 2012-01-15 07:58:46

+0

嗨keyboardP。再次感谢。因为你的例子,我用'*'得到了诀窍。非常感谢...你做了我的一天:-) – Nasenbaer 2012-01-15 08:57:15

+0

不客气: )'*'是通配符,因此它将匹配所有内容。 – keyboardP 2012-01-15 13:53:08