C# equivalent of getClassLoader().getResourceAsStream(…)

前端 未结 1 657
野趣味
野趣味 2021-01-14 09:42

In Java you can read a file embedded inside a JAR-file by using the following code:

String file = \"com/company/package/filename.txt\";
InputStream is = Clas         


        
1条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 10:15

    Once you've added the text file as a resource, and assigned a resourceName to it, then:

     Assembly assembly = Assembly.GetExecutingAssembly();
     TextReader inputStream = new StreamReader(assembly.GetManifestResourceStream(resourceName));
     string result = inputStream.ReadToEnd();
    

    Note: this came from this posting

    0 讨论(0)
提交回复
热议问题