working with app.config
-
Thursday, August 30, 2012 2:10 PM
In a C# 2010 application, I want to place files in dynamic folder structures that I create in the application. I want to create these dynamic locations so we can place data in the dynamic paths based upon customer name. I also wantto place these dynamic paths in the app.config file.
Thus can you tell me if there is a way to point the app.config file to these dynamic paths? If so can you tell me and/or point me to a reference I can use that will help me accomplish this goal?
All Replies
-
Thursday, August 30, 2012 3:15 PM
Hi Wendy,
Please read this thread, it has a similar problem like yours:
Basically, you need to create a simple c# class for that dynamic path to hold its properties, and in your app.config, declare a new config section which point to that class so ASP.NET will recognize your new class as a valid section. If everything setup correctly, you can use configuration manager to access your newly custom section.
Hope this help.
Sam.
-
Friday, August 31, 2012 3:43 AM
That is not exactly what I want to do.
I am asked to come up with dynamice directory paths that look like the followiing
C://08-30-2012/Customer 1
C://08-30-2012/customer 2
C://08-31-2012/ customer 1
C://08-31-2012/customer 3.
If I can not change the app.config file, how would you change code to come up directory path names like I am just displaying?
-
Friday, August 31, 2012 6:08 AM
Here´s a thought for you.
Use a small database, store the paths there, then iterate through the collections when needed.
Easy to maintain, expand and so forth.
//SFP
-
Friday, August 31, 2012 8:28 AM
Hi Wendy,
You can use App.Config file for this. You can create a new setting under appSettings section in App.Config file as below,
<?xml version="1.0"?> <configuration> <appSettings> <add key = "DynamicLocations"
value = "C://08-30-2012/Customer 1
C://08-30-2012/customer 2 C://08-31-2012/customer 1 C://08-31-2012/customer 3"/> </appSettings> </configuration>Then in C#, you can get these locations by calling below function.
private static List<string> ReadDynamicLocations() { List<string> result = new List<string>(); if (ConfigurationManager.AppSettings.AllKeys.Contains("DynamicLocations")) { string locationStrings = ConfigurationManager.AppSettings["DynamicLocations"]; List<string> locations = locationStrings.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .ToList(); locations.ForEach(loc => result.Add(loc.Trim())); } return result; }Note: Please modify the code according to your need.
I hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
-
Saturday, September 01, 2012 1:44 AMI do not understand your answer. I want the code to come up with the dynamic locations. I do not want hard-coded value paths in the app.config file. is the hard coded values in the app.config file setup to show me what values I can end up with?
-
Saturday, September 01, 2012 2:36 AM
Web.config file can reference an External Config File.
web.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings file="Custom.config"> ... </appSettings> <system.web> ... </system.web> </configuration>
Custom.config
<appSettings> <add key = "Location1" value = "C://08-30-2012/Customer 1"/> <add key = "Location2" value = "C://08-30-2012/customer 2"/> <add key = "Location3" value = "C://08-31-2012/customer 1"/> <add key = "Location4" value = "C://08-31-2012/customer 3"/> </appSettings>
I hope this will help you defining dynamic location.
-
Saturday, September 01, 2012 3:43 PM
Can you tell me what an external app.config file does solve solve this problem?
Also if I mention web.config file earlier, that was accidently. My first 2 assignments at my current job were working on a web application.
Now my company assigned me to work on a desktop/console application that I have been asking my few questions about.
-
Saturday, September 01, 2012 4:57 PMModerator
Hi Wendy,
I have a question for you ... once your code figures out the dynamic paths, is it your intention that these paths be stored in the .config and saved for future use? Or are these paths totally dynamic in that they will not be used for subsequent re-running of your app? IOW, are the paths only relevant to the current running of the app and will not be used the next time the app runs?
If you only need the paths while the app is running, and you don't need them for the next time the app is run, you can "save" these paths in a static List< > so that they can be accessed throughout the app, rather than putting them in a .config file.
~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.com -
Saturday, September 01, 2012 5:12 PM
I do only need the files for the length of the run? But I only need the files while I am accessing them one time.
Thus can you tell me how to use the static directory paths you suggested and/or how to use the dymanic paths?
-
Saturday, September 01, 2012 8:14 PMModerator
You can either have a separate class that you use to hold static properties, or you can simply put a static property in your WinForm or your Console. And, now that I think of it, you'd probably want a Dictionary rather than a List (unless all you need to do is iterate through the paths, then a List would be fine). Here's something off the top of my head with a Dictionary static property that should get you going in the right direction:
public class MyStaticStuff { private static Dictionary<string, string> m_Paths = null; public static Dictionary<string, string> Paths { get { if (m_Paths == null) m_Paths = new Dictionary<string, string>(); return m_Paths; } } } // You don't create an instance of this class, // static properties do not "belong" to an instance. // use it like this in your Form or Console or wherever: private void CreateMyPath() { string key = // some way of keeping track of which path is which string path = //code here to come up with your dynamic path name // then add that path to the static List MyStaticStuff.Paths.Add(key, path); } private string GetMyPath(string key) { return MyStaticStuff.Paths[key]; }
Hope that helps ...
~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.com- Marked As Answer by wendy elizabeth Sunday, September 02, 2012 3:47 AM

