从程序集中读取嵌入文件
我必须将配置文件的路径传递给框架方法(Gurok SmartInspect)。配置文件是程序集的嵌入资源。目前,我从程序集中读取文件并将其存储在外部,然后传递路径名称。有没有更好/更简单的方法来实现这个目标,而不复制文件?
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);
}
}
}
I have to pass the path of a config file to a framework method (Gurok SmartInspect). The config file is an embedded resource of the assembly. Currently I read the file from the assembly and store it outside and then pass the pathName. Is there a better / less complicated way to achieve this goal, 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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 Gurok SmartInspect 读取配置信息的唯一方法是从您向其传递路径的文件,并且您决定将该文件嵌入到程序集中,那么是的,您的方法没问题。您可能需要考虑添加一些异常处理,但除此之外我认为这没有问题。
If the only way that Gurok SmartInspect reads configuration information is from a file that you pass it a path to and you've decided to embed that file in your assembly, then yes, your method is fine. You might want to consider adding some exception handling but otherwise I see no problem with this.