How to use App.config in Visual Studio C# .NET
Add reference to System.Configuration :
and include it using the using statement:
using System.Configuration;
Edit App.config so that you add your settings in the appSettings node in an add element with key and value parameters.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add key="server" value="localhost" /> <add key="port" value="5432" /> <add key="username" value="myusername" /> <add key="password" value="mypass" /> <add key="database" value="mydb" /> </appSettings> </configuration>
Now you can access your settings in code like this:
string server = ConfigurationManager.AppSettings["server"].ToString(); string port = ConfigurationManager.AppSettings["port"].ToString(); string username = ConfigurationManager.AppSettings["username"].ToString(); string password = ConfigurationManager.AppSettings["password"].ToString(); string database = ConfigurationManager.AppSettings["database"].ToString();
Straightforward and to the point, just what I needed. Thanks Nikola!
You’re welcome Brian, I’m glad this proved to be helpful to you!
thanks a lot, so simple
Thanks, glad it helped!
how can i access app.config, I am unable to find.
I have added the reference, but i am not aware where to update my app settings code.
my code which I want to put:
i suppose you are using .net Core (2.0) framework
do that in web.config file in your project
Thank you helped lot
You’re welcome Shan, glad I helped!
What happens when you save your application in C:\Program Files or C:\Program Files (x86). The program does not have rights to create or edit files in there.
Conection Timeout?