gpt4 book ai didi

.net - 关于在 .NET Win 应用程序中存储应用程序设置的位置的困惑

转载 作者:行者123 更新时间:2023-12-01 08:55:52 24 4
gpt4 key购买 nike

我需要知道应用程序设置(连接字符串)可以存储在何处以及如何存储,以便它们可以在运行时更改和保存。

我知道在 VS 中,您可以在“项目”>“属性”下配置设置,这些设置存储在应用程序安装目录下的 appname.exe.config 文件中。但是“应用程序范围”在运行时不可读/写,如果您更改用户范围内的配置文件的副本在用户目录下创建,应用程序的其他用户将无法访问。

我需要有一种方法,让用户可以根据他们在应用程序中的需要配置存储在通用配置文件中的连接字符串,然后让所有其他用户(在该机器上)也可以使用它。我怎样才能做到这一点?

最佳答案

最简单、最快的解决方案是在普通用户有权读/写的共享位置创建配置文件。

为您的配置数据创建一个具有公共(public)属性的类,然后在这个共享位置将其序列化为 xml。

public class Configuration
{
// config filename
private static _name = Path.Combine(
System.Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData),
@"MyApp\MyConfig.xml");

// the connection string
public string ConnectionString {get;set;}

// load the configuration from disk
public static Configuration Load()
{
using (var f = File.OpenRead(name))
{
var x = new System.Xml.Serialization.XmlSerializer(typeof(Configuration));
return x.Deserialize(f) as Configuration;
}
}

// save the configuration to disk
public static Save(Configuration config)
{
using (var f = File.OpenWrite(name))
{
var x = new System.Xml.Serialization.XmlSerializer(typeof(Configuration));
x.Serialize(f, config);
}
}
}

关于.net - 关于在 .NET Win 应用程序中存储应用程序设置的位置的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1137724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com