我正在尝试允许用户在文本框中输入数据,该文本框将添加到web.config文件中。我已经在web.config文件中添加了相关的行,但是当我创建这个类时,一切都出错了。
每当我尝试运行我的应用程序时,我总是收到“are you missing a using指令”或“程序集引用”错误。我看过其他被问到这个问题的时候,似乎找不出我错在哪里。问题是,我是Visual Studio的新手,对可能的答案一无所知。
下面是生成错误的类文件。我希望我已经包括了你需要帮助我的一切。谢谢。
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
namespace WebConfigDemo
{
public class CompanyConfigSection : ConfigurationSection
{
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public CompanyConfigCollection Companies
{
get
{
return (CompanyConfigCollection)this[""];
}
set
{
this[""] = value;
}
}
}
public class CompanyConfigElement : ConfigurationElement
{
[ConfigurationProperty("id", IsKey = true, IsRequired = true)]
public int Id
{
get
{
return (int)this["id"];
}
set
{
this["id"] = value;
}
}
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return this["name"].ToString();
}
set
{
this["name"] = value;
}
}
} '
public class CompanyConfigCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new CompanyConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CompanyConfigElement)element).Id;
}
}
public class CompaniesConfig
{
private static readonly Dictionary<int, CompanyConfigElement>
Elements;
static CompaniesConfig()
{
Elements = new Dictionary<int, CompanyConfigElement>();
var section = (CompanyConfigSection)ConfigurationManager.GetSection ("companies");
foreach (CompanyConfigElement system in section.Companies)
Elements.Add(system.Id, system);
}
public static CompanyConfigElement GetCompany(int companyId)
{
return Elements[companyId];
}
public static List<CompanyConfigElement> Companies
{
get
{
return Elements.Values.ToList();
}
}
}
} '任何帮助我们都将不胜感激
发布于 2013-06-27 21:22:20
您可能没有将System.Configuration dll添加到项目引用中。默认情况下它不在那里,您必须手动添加它。
右键单击引用并在.net程序集中搜索System.Configuration。
看看你的推荐信里有没有...

右键单击并选择添加引用...

在.Net程序集列表中找到System.Configuration,选择它,然后单击Ok...

程序集现在应该出现在您的引用中...

发布于 2017-04-20 13:54:43
引用dll的.Net框架应与引用dll的项目的.Net框架版本相同
发布于 2022-02-07 19:19:31
如果您尝试过上述解决方案,但没有找到答案,请确保所有项目的.NET版本都是相同的。
在将.NET版本4.6.1导入到.NET版本4.6.2项目中时,我遇到了这个问题。没有任何来自Visual Basic的警告!
https://stackoverflow.com/questions/17344295
复制相似问题