Reading an embedded file from an assembly

I need to pass the path of the configuration file to the framework method (Gurok SmartInspect). The configuration file is an embedded build resource. I am currently reading a file from an assembly and saving it outside, and then passing the path Name. Is there a better / less complicated way to achieve this without copying the file?

private static void ConfigLogger() { const string embeddedFileName = "xxx.SmartInspect.properties"; const string configFileName = "SmartInspect.properties"; ExtractFileFromAssembly(embeddedFileName, configFileName); SiAuto.Si.LoadConfiguration(configFileName); } private static void ExtractFileFromAssembly(string assemblyFileName, string configFileName) { using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(assemblyFileName) ) { byte[] buffer = new byte[s.Length]; int read = s.Read(buffer, 0, (int)s.Length); using (FileStream fs = new FileStream(configFileName, FileMode.Create)) { fs.Write(buffer, 0, buffer.Length); } } } 
+1
source share
1 answer

If the only way Gurok SmartInspect reads configuration information is through a file through which you pass the path to it, and you decide to insert this file into your assembly, then yes, your method is fine. You might want to consider adding exception handling, but otherwise I don't see a problem with that.

+1
source

Source: https://habr.com/ru/post/1215412/


All Articles