如何获取所有文本文件资源名称和文件内容

时间:2013-01-08 16:46:02

标签: vb.net visual-studio visual-studio-2012 text-files my.resources

我需要做这样的事情:

   Private Sub SearchInResources(ByVal PatternSTR As String)
       For Each ResourceFile In My.Resources ' Obviously this don't works
           If ResourceFile.EndsWith(".txt") Then
               Dim fileContent As String = ResourceFile
               Dim stringStream As New System.IO.StringReader(fileContent)
               If stringStream.contains(PatternSTR) Then
                   ' Things...
               End If
           End If
       Next
   End Sub

我尝试过这种方法,但不适合我! :How to get the names of all resources in a resource file

2 个答案:

答案 0 :(得分:3)

这是方式:

    For Each ResourceFile As DictionaryEntry In My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True).OfType(Of Object)()
        If TypeOf (ResourceFile.Value) Is String Then
            MsgBox(My.Resources.ResourceManager.GetObject(ResourceFile.Key))
            'MsgBox(ResourceFile.Key)   ' Resource Name
            'MsgBox(ResourceFile.Value) ' Resource FileContent
        End If
    Next

答案 1 :(得分:1)

您可以将文件存储在文件夹中,然后通过

访问它们
For Each Content As String In My.Computer.FileSystem.GetFiles("D:\Resources", FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
    ListBox1.Items.Add(Content)
Next

只是您问题的替代解决方案