我正在尝试构建一个简单的Windows表单,它应该能够将值写入YAML配置文件。发布后,exe文件将保存在config目录中。
private void buttonSaveSettings_Click(object sender, EventArgs e)
{
string hostname = hostnameBox.Text;
var path = "C:\\Users\\Nick\\Desktop\\packages\\doctrine.yaml";
var deserializer = new YamlDotNet.Serialization.Deserializer();
try
{
using (var reader = new StreamReader(path))
{
var obj = deserializer.Deserialize<Dictionary<object, object>>(reader);
var doctrine = (Dictionary<object, object>)obj["doctrine"];
var dbal = (Dictionary<object, object>)doctrine["dbal"];
var connections = (Dictionary<object, object>)dbal["connections"];
var dict = (Dictionary<object, object>)connections["default"];
// TODO: Modify the dictionary
// dict["dbname"] = "test";
dict["dbname"] = "test";
using (StreamWriter writer = new StreamWriter(path))
{
// Save Changes
var serializer = new YamlDotNet.Serialization.Serializer();
serializer.Serialize(writer, obj);
}
}
}
catch (Exception exception)
{
dumpBox.Text = exception.Message;
}
}
这是Doctrine的配置文件(packages/doctrine.yaml)
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: dbname
user: user
password: password
host: host
driver: pdo_mysql
server_version: '10.0.38-MariaDB'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '5.7'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
我不确定是否仅使用System.IO就能处理该任务,因为我们可能需要一些额外的解析器。
问题是,我该如何重写属于doctrine/dbal/connections/default的每个键的值?
using (reader var...)
,我忘记了它,但由于它们是嵌套的,所以它没有关闭,似乎Linux有更好的文件管理,所以它在那里工作,我绝对很蠢,我非常高兴它能够工作。 - Jorge Alejandro