c# - 如何在我的代码中访问 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”。
解决方案
你不能在类级别编写这样的代码。constructor
您可以通过重载 the或 the来修复它Form_Load
namespace Black
{
public partial class frmMain : Form
{
string sStayOnTop;
public frmMain()
{
InitializeComponent();
sStayOnTop = ConfigurationManager.AppSettings["StayOnTop"];
}
}
}
推荐阅读
- java - 如何在给定特定类的情况下反转 LinkedList:代码未提供所需的输出
- python - 如何找到两个不同列表中每个元素的平均值
- php - Facebook 营销 - 故事图片
- node.js - API从网关传递后超时,因此无法在客户端获得结果
- postgresql - PostgreSQL - 更改列时无法将 varchar 转换为枚举类型
- c - C程序在fork后挂起
- javascript - Typescript - React 中的多类型情况如何
- oracle - 使用 Oracle APEX 进行流程跟踪
- java - 使用 Criteria Builder 过滤联接表
- java - 如何在 Android 中绘制带有渐变的弧线?