Equivalente C # di getClassLoader (). GetResourceAsStream (& # 8230;)
-
19-08-2019 - |
Domanda
In Java puoi leggere un file incorporato in un file JAR usando il seguente codice:
String file = "com/company/package/filename.txt";
InputStream is = ClassName.class.getClassLoader().getResourceAsStream(file);
Qual è l'equivalente C # /. NET del codice precedente, ovvero come posso leggere un file che ho incorporato in una DLL?
Grazie!
Soluzione
Dopo aver aggiunto il file di testo come risorsa e assegnato un nome risorsa
, quindi:
Assembly assembly = Assembly.GetExecutingAssembly();
TextReader inputStream = new StreamReader(assembly.GetManifestResourceStream(resourceName));
string result = inputStream.ReadToEnd();
Nota: questo proviene da questo pubblicazione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow