我正在开发一个 C# 控制台应用程序。我想包含一些属性文件,该文件具有名称值排序 pf 对,我可以在运行时使用并且可以像 java 中的 config.properties 一样进行编辑有什么建议吗?
只需将 App.config 文件添加到项目中即可
然后设置配置文件以保存属性值
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Property1" value="value1"/>
<add key="Property2" value="value2"/>
<add key="Property3" value="value3"/>
</appSettings> </configuration>
之后,需要在项目的解决方案资源管理器中添加对 .NET System.Configuration
程序集的引用(如果尚未引用它)。
然后你可以做这样的事情
using System;
using System.Configuration;
namespace YourAppNamespace
{
class Program
{
static void Main(string[] args)
{
string property1 = ConfigurationManager.AppSettings["Property1"];
string property2 = ConfigurationManager.AppSettings["Property2"];
string property3 = ConfigurationManager.AppSettings["Property3"];
Console.WriteLine(property1);
Console.WriteLine(property2);
Console.WriteLine(property3);
}
}
}