Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2005, 04:37 PM
Authorized User
 
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# Config File

I am a long time php programmer and I have recently dove into the C# mess here. I am building an application that where I want to create a config file with database user/pass/server variables etc that can easily be configured by an end user looking to configure the app for their system, and still have my variables global to the application. I want them to be outside the compiled code. The global.asax.cs file works, but it is compiled and not easily modifiable by an end user. What is the best solution?

Thanks!

 
Old January 5th, 2005, 06:17 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The .config files are built for this purpose.

For a web application, there is a web.config file and for a form/console application there is an app.config. (When you compile a form/console application in visual studio, the app.config file is renamed <myapplication>.config. .NET automatically looks for such a file when you run the EXE.)

Inside the config file you can place key value pairs. Usually this is where people put connection strings and the like.
 
Old January 6th, 2005, 10:38 AM
Authorized User
 
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is it possible to reference multiple .config files so you can leave your <appSettings> keys in a different file?

 
Old January 6th, 2005, 10:42 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What are you hoping to achieve?

You don't explicitly reference .config files, .NET does it for you. You can put settings in the machine.config, then in the application config file. In the case of a web app you would have a master web.config in the root, then you could have any number of web.config files in subdirectories of the web application. There are certain config nodes that you can't put in the 'secondary' web.config files, but the appSettings section is allowed.
 
Old January 7th, 2005, 09:58 AM
Authorized User
 
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was just wondering if you can organize your appsettings in a different file. It sounds like it is possible. Thanks a bunch for your help.

73

 
Old January 7th, 2005, 05:32 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can always write your own AppSettings class. Just use some methods from the System.Xml namespace and read in your files.

If you design such a class, it's a good idea to use ASP.NET caching to avoid reading the file for each request.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: The Hardest Button To Button by The White Stripes (Track 9 from the album: Elephant) What's This?
 
Old January 11th, 2005, 10:38 AM
Authorized User
 
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I actually decided to finally throw it into the web.config file, but I am curious to learn more about your class idea, as I am new to the .Net world. I do not want these settings to reside inside the compiled code. Is there a way to create a class that is not compiled? I am under the impression that all classes are compiled.

 
Old January 13th, 2005, 11:16 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're correct about classes always being compiled in .NET.

What I was referring to was a (compiled) class that accesses your custom file and return its values.
That would give you a programming model similar to the AppSettings class (which basically does the same thing) but with your own custom files.

Then you could do things like this:
Code:
Dim sConnection As String
sConnection = Helpers.AppSettings.Get("ConnectionString")
The Get method of the AppSettings class in your Helpers namespace would then read in your custom config file (you could use the default Web.Config to store the location(s) of your custom config files) and retrieve the required key.
For optimal performance, the returned values can be cached, so you can return them from the cache rather than hitting the file again on each subsequent request. The cache will be automatically invalidated when your main Web.Config file changes, or you can build in some code to invalidate the cache when relevant (built a file dependency on your own config files).


Does this help?

Imar

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 17th, 2005, 12:26 PM
Authorized User
 
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will give it a whirl. Thanks a bunch!

Jeff







Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with the Application.config file chobo2 C# 2008 aka C# 3.0 4 November 13th, 2008 07:47 AM
Problems with web.config file !!!!!!!! _fluffy_ BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 October 14th, 2006 10:02 AM
web.config file apry BOOK: Beginning ASP.NET 1.0 1 May 27th, 2004 06:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.