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 展开
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 展开
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>
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>
展开全部
ConfigurationManager 类
提供对客户端应用程序配置文件的访问。无法继承此类。
命名空间: System.Configuration
程序集: System.Configuration(在 System.Configuration.dll 中)
你添加有System.Configuration.dll没有?添加了就会有那个了ConfigurationManager
提供对客户端应用程序配置文件的访问。无法继承此类。
命名空间: System.Configuration
程序集: System.Configuration(在 System.Configuration.dll 中)
你添加有System.Configuration.dll没有?添加了就会有那个了ConfigurationManager
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询