编译器找不到 app.config 中的属性
The compiler cannot find the attributes in the app.config
编译器找不到 app.config 中的属性(他抛出 SettingsPropertyNotFoundException )。
这是代码:
class Program
{
static void Main(string[] args)
{
ConfigurationFile settings = new ConfigurationFile();
settings.SoundFile = "ring.wav";
settings.BackgroundColor = Color.Red;
settings.Save();
Console.WriteLine("finished");
Console.ReadLine();
}
}
sealed internal partial class ConfigurationFile : global::System.Configuration.ApplicationSettingsBase
{
public string SoundFile
{
get
{
return ((string) this["SoundFile"]);
}
set
{
this["SoundFile"] = value;
}
}
public global::System.Drawing.Color BackgroundColor
{
get
{
return ((global::System.Drawing.Color) this["Background"]);
}
set
{
this["Background"] = value;
}
}
}
我最初以为我会选择与我的 app.config 文件不同的其他名称,但它们是相同的。这是我第一次使用 app.config 所以我想我做错了什么。如果有人能帮助我,我会很高兴。
您不必以这种方式使用设置。如果你[右键单击你的项目 -> 添加 -> 新项目 -> 设置文件(Settings1.settings 将是默认名称) -> 配置你的属性] 你可以像这样进一步使用它
static void Main(string[] args)
{
Settings1.Default.Background = Color.Red;
Settings1.Default.SoundFile = "ring.wav";
Settings1.Default.Save();
Console.WriteLine("finished");
Console.ReadLine();
}
您尝试使用的设置是不同于应用程序配置设置的项目设置。
您还需要使用以下任一属性标记属性:
[System.Configuration.ApplicationScopedSetting]
public string SoundFile
// 或
[System.Configuration.UserScopedSetting]
public string SoundFile
保存只会保存项目属性部分下 Settings.settings 中的更改。
可以通过以下方式访问应用程序配置设置(您还需要在项目中添加对 System.Configuration 程序集的引用):
System.Configuration.ConfigurationManager.AppSettings["SoundFile"];
如果你想在你的配置文件中使用 ApplicationConfiguration 那么你可以这样做:
public string SoundFile
{
get
{
return ConfigurationManager.AppSettings["SoundFile"];
}
set
{
ConfigurationManager.AppSettings["SoundFile"] = value;
}
}
它不会将值保存到文件中,但会在当前会话期间保留更新的值。
编译器找不到 app.config 中的属性(他抛出 SettingsPropertyNotFoundException )。 这是代码:
class Program
{
static void Main(string[] args)
{
ConfigurationFile settings = new ConfigurationFile();
settings.SoundFile = "ring.wav";
settings.BackgroundColor = Color.Red;
settings.Save();
Console.WriteLine("finished");
Console.ReadLine();
}
}
sealed internal partial class ConfigurationFile : global::System.Configuration.ApplicationSettingsBase
{
public string SoundFile
{
get
{
return ((string) this["SoundFile"]);
}
set
{
this["SoundFile"] = value;
}
}
public global::System.Drawing.Color BackgroundColor
{
get
{
return ((global::System.Drawing.Color) this["Background"]);
}
set
{
this["Background"] = value;
}
}
}
我最初以为我会选择与我的 app.config 文件不同的其他名称,但它们是相同的。这是我第一次使用 app.config 所以我想我做错了什么。如果有人能帮助我,我会很高兴。
您不必以这种方式使用设置。如果你[右键单击你的项目 -> 添加 -> 新项目 -> 设置文件(Settings1.settings 将是默认名称) -> 配置你的属性] 你可以像这样进一步使用它
static void Main(string[] args)
{
Settings1.Default.Background = Color.Red;
Settings1.Default.SoundFile = "ring.wav";
Settings1.Default.Save();
Console.WriteLine("finished");
Console.ReadLine();
}
您尝试使用的设置是不同于应用程序配置设置的项目设置。 您还需要使用以下任一属性标记属性:
[System.Configuration.ApplicationScopedSetting]
public string SoundFile
// 或
[System.Configuration.UserScopedSetting]
public string SoundFile
保存只会保存项目属性部分下 Settings.settings 中的更改。
可以通过以下方式访问应用程序配置设置(您还需要在项目中添加对 System.Configuration 程序集的引用):
System.Configuration.ConfigurationManager.AppSettings["SoundFile"];
如果你想在你的配置文件中使用 ApplicationConfiguration 那么你可以这样做:
public string SoundFile
{
get
{
return ConfigurationManager.AppSettings["SoundFile"];
}
set
{
ConfigurationManager.AppSettings["SoundFile"] = value;
}
}
它不会将值保存到文件中,但会在当前会话期间保留更新的值。