What is the best way to get data from app.config?

Over time, I used:

ConfigurationManager.ConnectionStrings["sqlconnectionstring"].ConnectionString

to get the connection string from the app.config file

<configuration>
   <connectionStrings>
       <add name="sqlconnectionstring" connectionString="Data  Source=ggg;Initial Catalog =DB;User ID=sa;Password=sa" />
   </connectionStrings>
</configuration>

Recently, I found that I can create a string using

global::myProj.Properties.Settings.Default.sqlconnectionstring;
  • What is the difference?
  • Is it possible to access the key in appSetting?
  • Why don't I need to import System.Configuration?

Thanks Asaf

+3
source share
3 answers

Material global::myProj.Properties.Settings.Default.sqlconnectionstring;is created by the parameter constructor in Visual Studio.

Settings can be found in the solution explorer under "Properties-> Network Settings." Type type label methods are automatically generated for all your properties defined in the parameter constructor.

, ( ).

, .

  • - .
  • , , "applicationSettings".
  • (, , ) -).
+3

, global::myProj.Properties.Settings.Default Visual Studio, . , app.config.

, System.Configuration , System.configuration.dll. , System.Configuration (, System.dll), .

ConfigurationManager System.configuration.dll. , , .

appSetting app.config, , ConfigurationManager. , Visual Studio , global::myProj.Properties.

+1

global::, , .

# Properties.Settings.Default .

0
source

Source: https://habr.com/ru/post/1760403/


All Articles