PIC*_*ain 1 .net c# error-handling resource-files
我有一个用C#.NET编写的软件,它通过USB与设备通信.出现问题时,设备会设置一个错误代码,该错误代码由我的软件以整数形式读取.我想在我的C#程序中存储与特定固件错误代码相关的错误消息.存储这些的最佳方法是什么?
有人告诉我,一个好的方法是使用资源文件.使用资源文件有什么好处?有没有更好的方法呢?
我建议也使用资源文件.一个关键的好处是本地化(使用不同的语言).即使您今天不想使用多种语言,您也可以灵活地在以后添加它们.另一个好处是简单的.NET/c#集成.
var key = "134"; //error code
var resourceFile = "someFileName.resx";
var filePath = "c:\some\path\";
ResourceManager resourceManager = ResourceManager
.CreateFileBasedResourceManager(resourceFile, filePath, null);
resourceValue = resourceManager.GetString(key);
Run Code Online (Sandbox Code Playgroud)
此外,在更改消息时,很容易获得资源文件.