首页 > 解决方案 > 如何在我的代码中访问 System.Configuration 参考?

问题描述

我正在编写代码以将自定义变量存储到配置文件中。存在 black.exe.config XML 文件。按照本教程,应该可以从我的代码中访问该类。

我试图重建解决方案,但没有奏效。项目中引用了 System.Configuration。

black.exe.config:

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
           <add key="StayOnTop" value="true" />
        </appSettings>
</configuration>

Form1.cs:

using System;
using System.Configuration;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;


namespace Black
{
    public partial class frmMain : Form
    {
        string sStayOnTop;
        sStayOnTop = ConfigurationManager.AppSettings.Get("StayOnTop");

        ...

    }
}    

错误 CS1519 类、结构或接口成员声明中的标记“=”无效 //

错误 IDE1007 当前上下文中不存在名称“ConfigurationManager.AppSettings.Get”。

标签: c#

解决方案


你不能在类级别编写这样的代码。constructor您可以通过重载 the或 the来修复它Form_Load

namespace Black
{
    public partial class frmMain : Form
    {
        string sStayOnTop;

        public frmMain()
        {
            InitializeComponent();
            sStayOnTop = ConfigurationManager.AppSettings["StayOnTop"];
        }

    }
}  

推荐阅读