C# 中winform使用ConfigurationManager出错

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Data;usingSy... using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CK.my
{
public class connDB
{
private readonly string RETURNVALUE = "RETURNVALUE";
protected SqlConnection cn;
protected String ConnectionString;
public connDB()
{
ConnectionString = ConfigurationManager.ConnectionStrings["CK.Properties.Settings.xintang_DataConnectionString"].ConnectionString;
}

提示:当前上下文中不存在ConfigurationManager,这是怎么回事啊
用的是vs2005
展开
 我来答
百度网友547f201
2009-04-02 · TA获得超过100个赞
知道答主
回答量:144
采纳率:0%
帮助的人:74.2万
展开全部
// Show how to use AppSettings.
static void DisplayAppSettings()
{

// Get the AppSettings collection.
NameValueCollection appSettings =
ConfigurationManager.AppSettings;

string[] keys = appSettings.AllKeys;

Console.WriteLine();
Console.WriteLine("Application appSettings:");

// Loop to get key/value pairs.
for (int i = 0; i < appSettings.Count; i++)

Console.WriteLine("#{0} Name: {1} Value: {2}",
i, keys[i], appSettings[i]);

}

// Show how to use ConnectionStrings.
static void DisplayConnectionStrings()
{

// Get the ConnectionStrings collection.
ConnectionStringSettingsCollection connections =
ConfigurationManager.ConnectionStrings;

Console.WriteLine();
Console.WriteLine("Connection strings:");

// Loop to get the collection elements.
IEnumerator conEnum =
connections.GetEnumerator();

int i = 0;
while (conEnum.MoveNext())
{
string name = connections[i].Name;
string connectionString = connections[name].ConnectionString;
string provider = connections[name].ProviderName;

Console.WriteLine("Name: {0}", name);
Console.WriteLine("Connection string: {0}", connectionString);
Console.WriteLine("Provider: {0}", provider);
}

}

// Show how to use OpenMachineConfiguration.
static void DisplayMachineConfigSections()
{
// Get the machine.config file.
Configuration machineConfig =
ConfigurationManager.OpenMachineConfiguration();

ConfigurationSectionCollection sections = machineConfig.Sections;

Console.WriteLine();
Console.WriteLine("Sections in machine.config:");

// Loop to get the sections machine.config.
foreach (ConfigurationSection section in sections)
{
string name = section.SectionInformation.Name;
Console.WriteLine("Name: {0}", name);
}

}

// Show how to use OpenExeConfiguration(ConfigurationUserLevel)
// and RefreshSection.
static void UpdateAppSettings()
{
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);

// Add an entry to appSettings.
int appStgCnt =
ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString();

string newValue = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();

config.AppSettings.Settings.Add(newKey, newValue);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");

}

// Show how to use OpenExeConfiguration(string).
static void DisplayAppSettingsRawXml()
{

// Get the application path.
string exePath = System.IO.Path.Combine(
Environment.CurrentDirectory, "ConfigurationManager.exe");

// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(exePath);

// Get the AppSetins section.
AppSettingsSection appSettingSection = config.AppSettings;

// Display raw xml.
Console.WriteLine(appSettingSection.SectionInformation.GetRawXml());

}

// Show how to use GetSection.
static void DisplayAppSettingsSectionRawXml()
{

// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the AppSetins section.
AppSettingsSection appSettingSection =
(AppSettingsSection)config.GetSection("appSettings");

// Display raw xml.
Console.WriteLine(appSettingSection.SectionInformation.GetRawXml());

}

// Show how to use OpenMappedMachineConfiguration.
static void DisplayMappedMachineConfigurationFileSections()
{
// Get the machine.config file.
Configuration machineConfig =
ConfigurationManager.OpenMachineConfiguration();

// Map to the machine configuration file.
ConfigurationFileMap configFile =
new ConfigurationFileMap(machineConfig.FilePath);
Configuration config =
ConfigurationManager.OpenMappedMachineConfiguration(configFile);

// Display the configuration file sections.
ConfigurationSectionCollection sections = config.Sections;

Console.WriteLine();
Console.WriteLine("Sections in machine.config:");

// Loop to get the sections machine.config.
foreach (ConfigurationSection section in sections)
{
string name = section.SectionInformation.Name;
Console.WriteLine("Name: {0}", name);
}

}

// Show how to use OpenMappedExeConfiguration.
static void DisplayMappedExeConfigurationFileSections()
{
// Get the application configuration file path.
string exeFilePath = System.IO.Path.Combine(
Environment.CurrentDirectory, "ConfigurationManager.exe.config");

// Map to the application configuration file.
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = exeFilePath;
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(configFile,
ConfigurationUserLevel.None);

// Display the configuration file sections.
ConfigurationSectionCollection sections = config.Sections;

Console.WriteLine();
Console.WriteLine("Sections in machine.config:");

// Loop to get the sections machine.config.
foreach (ConfigurationSection section in sections)
{
string name = section.SectionInformation.Name;
Console.WriteLine("Name: {0}", name);
}

}

static void Main(string[] args)
{

// Show how to use OpenExeConfiguration() and RefreshSection.
UpdateAppSettings();

// Show how to use AppSettings.
DisplayAppSettings();

// Show how to use ConnectionStrings.
DisplayConnectionStrings();

// Show how to use OpenExeConfiguration(string).
DisplayAppSettingsRawXml();

// Show how to use GetSection.
DisplayAppSettingsSectionRawXml();

// Show how to use OpenMappedMachineConfiguration.
DisplayMappedMachineConfigurationFileSections();

// Show how to use OpenMappedExeConfiguration.
DisplayMappedExeConfigurationFileSections();

// Show how to use OpenMachineConfiguration.
DisplayMachineConfigSections();

}
}
用的是这个xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="NewKey0" value="Monday, January 23, 2006 2:56:14 PM" />
<add key="NewKey1" value="Monday, January 23, 2006 3:15:18 PM" />
<add key="NewKey2" value="Monday, January 23, 2006 3:16:29 PM" />
</appSettings>
</configuration>
定复2d
推荐于2016-10-02 · TA获得超过3196个赞
知道大有可为答主
回答量:2052
采纳率:0%
帮助的人:1935万
展开全部
ConfigurationManager 类

提供对客户端应用程序配置文件的访问。无法继承此类。

命名空间: System.Configuration
程序集: System.Configuration(在 System.Configuration.dll 中)

你添加有System.Configuration.dll没有?添加了就会有那个了ConfigurationManager
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式