我有一个列表框,该列表框包含多个字符串值。我想将这些字符串添加到配置文件,并希望从配置中读取值并将其放在列表框中。该方法应该是什么?我是配置文件的新手。
答案 0 :(得分:1)
以下代码段将帮助您修改项目的app.config文件。
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Remove("MySetting");
config.AppSettings.Settings.Add("MySetting", "some value");
config.Save(ConfigurationSaveMode.Modified);
要阅读配置值,您只需使用ConfigurationManager.AppSettings["MySetting"]
答案 1 :(得分:0)
您可以通过app.config文件存储和检索应用的配置数据。
您需要在代码中添加对System.Configuration的引用,并将System.Configuration程序集添加到您的应用程序中。
这是一篇包含更多信息的好帖子 - What is App.config in C#.NET? How to use it?
答案 2 :(得分:0)
可能您希望将这些值存在于文件中,然后从该文件中读取并附加到列表框中,如
string path = @"c:\temp\MyTest.txt";
if (File.Exists(path))
{
string[] readText = File.ReadAllLines(path);
}
您可以附上您的列表框
listbox1.DataSource = new List<string>().AddRange(readText);