简体   繁体   中英

Accessing Properties.Settings.Default from Class?

When I create a Console App I can right click on the project in Visual Studio 2008 Pro. click Properties > Go To Settings enter a Application Scope Setting and then access it from code like

Console.WriteLine(Properties.Settings.Default.MySetting.ToString());

When I do the same thing in a Class Project and try to assign the the Applicatin Scope Setting to a variable like

this.mySettingVariable = Properties.Settings.Default.MySetting.ToString();

I get an the error when building that "The name 'Properties' does not exist in the current context."

What am I doing wrong and how can I access the Application Scoped properties for a class project?

You forgot to add settings to the class library project. You cannot access the settings of the EXE project directly.

Giving a class library project its own settings is possible but it is a hassle. A DLL cannot have its own .config file, there's only one: the app.exe.config file. You have to merge the entries in the DLL's app.config file into the app.exe.config file by hand. Copy and paste with a text editor. This is not very maintainable.

Another approach is to make these settings properties of the class. And let the code in the main project initialize them from its own settings. Or just ditch settings because their a pita and use an .xml file.

Make sure you have included the namespace in the 'usings'.

Also make sure the settings class is public.

Pulled from here

Because there is no configuration file model for class libraries, application settings do not apply for Class Library projects. The exception is a Visual Studio Tools for Office DLL project, which can have a configuration file.

您的.settings文件是否在项目的Properties文件夹下?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM