How to read embedded resource text file

前端 未结 22 2329
悲&欢浪女
悲&欢浪女 2020-11-21 06:03

How do I read an embedded resource (text file) using StreamReader and return it as a string? My current script uses a Windows form and textbox that allows the

22条回答
  •  借酒劲吻你
    2020-11-21 06:56

    For users that are using VB.Net

    Imports System.IO
    Imports System.Reflection
    
    Dim reader As StreamReader
    Dim ass As Assembly = Assembly.GetExecutingAssembly()
    Dim sFileName = "MyApplicationName.JavaScript.js" 
    Dim reader = New StreamReader(ass.GetManifestResourceStream(sFileName))
    Dim sScriptText = reader.ReadToEnd()
    reader.Close()
    

    where MyApplicationName is namespace of my application. It is not the assembly name. This name is define in project's properties (Application tab).

    If you don't find correct resource name, you can use GetManifestResourceNames() function

    Dim resourceName() As String = ass.GetManifestResourceNames()
    

    or

    Dim sName As String 
        = ass.GetManifestResourceNames()
            .Single(Function(x) x.EndsWith("JavaScript.js"))
    

    or

    Dim sNameList 
        = ass.GetManifestResourceNames()
            .Where(Function(x As String) x.EndsWith(".js"))
    

提交回复
热议问题