从 exe.config 读取appSettings 中的配置数据

右键解决方案,添加引用--> System.Configuration.dll

 

在exe.config 中添加数据

<appSettings>
    <add key="IP" value="0.0.0.0" />    
<add key="Port" value="1234" /> </appSettings>

 

读取添加的配置数据

if ((true == IPAddress.TryParse(ConfigurationManager.AppSettings["IP"], out IPAddress alarmIP))&& (true == int.TryParse(ConfigurationManager.AppSettings["Port"], out int alarmPort)))
{//有外部配置,用外部配置指定地址
       jTudpt = new JTudpt("UDP", alarmIP.ToString(), alarmPort);
}
else
{//无外部配置,用默认地址和端口
       jTudpt = new JTudpt("UDP", "0.0.0.0", 1236);//服务器
}

 

posted @ 2019-03-19 19:42  sethnie  阅读(449)  评论(0编辑  收藏  举报