可以将Ini文件存储在资源文件中吗?

huangapple go评论68阅读模式
英文:

Can I store an Ini file in a Resources file?

问题

我有一个Windows Forms应用程序,.Net Framework 4.6.1,我想将一些数据库连接数据存储在Ini文件中。

然后,我想将它存储在项目的资源文件中(这样我就不必手动复制/粘贴文件到Debug和Release文件夹中,等等)作为普通文件,但是当我尝试编译程序并使用ini-parser读取Ini数据时,出现了以下异常:'System.ArgumentException: '路径访问中的无效字符''。

我正在使用Properties.Resources来读取Ini文件,所以我猜想路径应该没有问题。这可能是Ini文件本身的问题吗?

Ini文件的内容如下:

[Db]
host = (一个IP)
port = (一个端口)
db = (一个数据库名称)
user = (一个数据库用户)
password = (一个数据库用户密码)

以及我读取数据的方法:

public static void ParseIniData()
{
    var parser = new FileIniDataParser();
    IniData data = parser.ReadFile(Properties.Resources.dbc);

    mysqlHost = data["Db"]["host"];
    mysqlPort = data["Db"]["port"];
    mysqlDb = data["Db"]["db"];
    mysqlUser = data["Db"]["user"];
    mysqlPwd = data["Db"]["password"];
}
英文:

I have a Windows Forms application, .Net Framework 4.6.1, and I want to store some DB connection data in an Ini file.

I then wanted to store it in the Resources file of the project (so I don't have to copy/paste the file in the Debug and Release folder manually, etc.) as a normal file, but when I tried to compile the program and read the Ini data with ini-parser, the following exception showed up: System.ArgumentException: 'Invalid characters in path access'.

I'm using Properties.Resources where I read the Ini file, so I guessed there would be no problem with the path. Could it be a problem with the Ini file itself?

The content of the Ini file is the following:

[Db]
host = (anIP)
port = (aPort)
db = (aDbName)
user = (aDbUser)
password = (aDbUserPwd)

And my method for reading the data:

public static void ParseIniData()
{
    var parser = new FileIniDataParser();
    IniData data = parser.ReadFile(Properties.Resources.dbc);

    mysqlHost = data["Db"]["host"];
    mysqlPort = data["Db"]["port"];
    mysqlDb = data["Db"]["db"];
    mysqlUser = data["Db"]["user"];
    mysqlPwd = data["Db"]["password"];
}

答案1

得分: 1

我最终成功地使用了@KlausG在评论中告诉我的方法(感谢!)。

不要使用FileIniDataParser,而是要使用StreamIniDataParser,并使用Assembly.GetManifestResourceStream获取流。

我觉得这有点棘手,因为使用这种方法,你需要将要读取的文件的Build Action设置为Embedded Resource。然后,这个文件会在编译时作为嵌入式资源添加,你可以检索到它的流。

所以我的方法最终如下:

public static void ParseIniData()
{
    var parser = new StreamIniDataParser();
    dbcReader = new StreamReader(_Assembly.GetManifestResourceStream("NewsEditor.Resources.dbc.ini"));
            
    IniData data = parser.ReadData(dbcReader);

    mysqlHost = data["Db"]["host"];
    mysqlPort = data["Db"]["port"];
    mysqlDb = data["Db"]["db"];
    mysqlUser = data["Db"]["user"];
    mysqlPwd = data["Db"]["password"];
}

其中_Assembly是一个私有的静态属性:private static Assembly _Assembly = Assembly.GetExecutingAssembly();。这会获取执行代码时正在运行的程序集(你也可以直接在方法中使用此代码,但我在类的另一个方法中使用了Assembly,所以我决定设置一个属性...我想这也是为了避免重复)。

英文:

I finally could do it using what @KlausGütter told me in the comments (thanks!).

Instead of using the FileIniDataParser you have to use the StreamIniDataParser, and get the Stream with Assembly.GetManifestResourceStream.

I found this a bit tricky, because using this method you need to set the Build Action in the file you want to read to Embedded Resource.
This file is then added as an embedded resource in compile time and you can retrieve its stream.

So my method ended up the following way:

public static void ParseIniData()
{
    var parser = new StreamIniDataParser();
    dbcReader = new StreamReader(_Assembly.GetManifestResourceStream("NewsEditor.Resources.dbc.ini"));
            
    IniData data = parser.ReadData(dbcReader);

    mysqlHost = data["Db"]["host"];
    mysqlPort = data["Db"]["port"];
    mysqlDb = data["Db"]["db"];
    mysqlUser = data["Db"]["user"];
    mysqlPwd = data["Db"]["password"];
}

where _Assembly is a private static attribute: private static Assembly _Assembly = Assembly.GetExecutingAssembly();. This gets you the assembly that's being executed when running the code (you could also use this code directly in the method, but I used the Assembly on another method in my class, so I decided to set an attribute... DRY I guess).

huangapple
  • 本文由 发表于 2020年1月3日 18:02:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定