我正在尝试使用 C# 在 Windows 窗体应用程序中使用新的设置文件。
删除旧的设置文件 (Settings1.settings) 并创建新的设置文件 (Settings.settings) 后。
当我开始编辑使用设置文件的代码时,我意识到新的设置文件无法被识别。示例:
我们公司创建的命名空间中使用了一个自定义设置类,这导致了一些冲突。如何将代码指向正确的设置文件?
最佳答案
在尝试复制您所做的事情时,我注意到 Settings.Designer.cs 文件的命名空间来自此(我正在使用控制台应用程序进行测试)。
namespace ConsoleApplication1.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool Test {
get {
return ((bool)(this["Test"]));
}
set {
this["Test"] = value;
}
}
}
}
至
namespace ConsoleApplication1 {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool Temp {
get {
return ((bool)(this["Temp"]));
}
set {
this["Temp"] = value;
}
}
}
}
如果您注意到命名空间从 ConsoleApplication1.Properties 变为 ConsoleApplication1,那么如果您将 .Properties 添加到新设置文件的命名空间,它应该可以解决您的问题。
关于c# - Visual Studio 将无法识别新的设置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11110839/