Click to See Complete Forum and Search --> : Config.xml


shers
January 19th, 2009, 07:41 AM
Hi,

I have done some code to display a form with a treeview. The initial folder in the treeview is given in the Project Properties Settings.
My intention is to allow the client or the administrator to change the initial path from the Config.xml file. But, surprisingly, when I tested the program from another PC on the network, it reads the path without the config.xml. How could that happen? How do I code to allow the administrator to change the intial path?

Thanks

toraj58
January 19th, 2009, 08:24 AM
first add a setting file to your project; for doing that follow this: in the solution explorer Right-Click on project name > add > new item > setting file.

then in the designer add this value to the settings:

Name: appPath
Type: string
Scope: User
Value: c:\

notice: in the setting file we can use two different scopes (User and Application) User Scopes are Readable and Writable but application scopes are readonly (it is only possible to change them in the generated xml file)

then in your code for reading and changing setting do something like this:


public Form1()
{
InitializeComponent();
//reading Initial Directory from setting file
openFileDialog1.InitialDirectory = Settings1.Default.appPath;
}

private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
MessageBox.Show("Testing Default Direcory");
}

private void button2_Click(object sender, EventArgs e)
{
//Changing Defaut Directory
Settings1.Default.appPath = textBox1.Text;
openFileDialog1.InitialDirectory = Settings1.Default.appPath;
}

shers
January 20th, 2009, 05:34 AM
I have done the same. But it doesn't work.

toraj58
January 20th, 2009, 07:21 AM
where is your problem;
i assume the your project name is test; check in the bin > debug folder to see that you have such file:

test.exe.config

if you have then open it in an app like notepad then copy and paste the content of it here to see what you have in your file.

shers
January 20th, 2009, 11:59 PM
My project name is BM. It is a dll project. I do have BM.dll.config in the bin > debug folder. I tried to change the path in the BM.dll.config file using Notepad, but he dll does not read the new path. Here is the content in the config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="BM.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<BM.Properties.Settings>
<setting name="ParentPath" serializeAs="String">
<value>C:\MyWorkFiles\</value>
</setting>
</BM.Properties.Settings>
</userSettings>
</configuration>

toraj58
January 21st, 2009, 02:13 AM
the XML format is correct, you shoud use following code to read and write the path:


Properties.Settings.Default.ParentPath;


also change this:

<value>C:\MyWorkFiles\</value>

to

<value>C:\myworkfiles\</value>

bacause unix-base systems are case sensitive for path and if you forget to use right case you may encounter run-time error.

shers
January 21st, 2009, 04:45 AM
the XML format is correct, you shoud use following code to read and write the path:


Properties.Settings.Default.ParentPath;



That is how I have coded. Here it is:

private string parPath = Properties.Settings.Default.ParentPath.ToString();


also change this:

<value>C:\MyWorkFiles\</value>

to

<value>C:\myworkfiles\</value>

bacause unix-base systems are case sensitive for path and if you forget to use right case you may encounter run-time error.

That is how the folder is named. So that is why I gave the name as it is. And I think you did not understand my question. It reads whatever is there in the app.config file when I am in the code editor. But when I move the dll and the .dll.config file to a different location and change the path in the dll.config file, it doesn't read. It displays the previous path that was there when the dll was executed in the code window.

Thanks

MadHatter
January 21st, 2009, 06:15 AM
settings like this retain a default value (the value you give it in the resource editor). user settings are stored in the users local storage (only the default value is stored in the app config, not what's actually used), not in the app config. if you change the path via code, you need to make sure to invoke the settings Save method after setting its value to something different. If the user changes the value by hand they need to change the value in the user.config file in the users local directory (in vista it's located at \Users\UserName\SoftwareCompanyName\ApplicationName\Version\user.config in xp it will be different but since I only have vista at the moment this is all I can provide)

If you set the setting as an application setting rather than a user setting it gets stored and loaded from the app config rather than the user config. therefore changing the value in the app.exe.config will change what's loaded in the program.

Also, dll's do not use config files... only exe files do. so if you're trying to pass these settings along to the running application, you have to add the app settings to the exe's config file. a dll's config file will always be ignored by the application.

toraj58
January 21st, 2009, 06:20 AM
When you run a program, dll files are introduced to memory for the running of the program. After the application using these same dll's has been closed, Windows Explorer caches these DLL files (Dynamic-Link Librarie) in memory for a period of time. This is an inefficient use of allotted memory. so inorder to find that the problem is not becuase of DLL caching you can check it again after the period of caching was elapsed.

toraj58
January 21st, 2009, 06:25 AM
If you set the setting as an application setting rather than a user setting it gets stored and loaded from the app config rather than the user config. therefore changing the value in the app.exe.config will change what's loaded in the program.


both user and application scope will be saved in the same file as [application_name].[exe][dll].config and application scope is readonly and con not be changed in the code it is only possible to change it in the mentioned file(manualy or by manipulating the file with code)

MadHatter
January 21st, 2009, 06:31 AM
the setting in the app config is only used if there is not a user.config. calling save on your Settings.Default will cause a user config to be saved. If there is no user config, the application setting may be changed through the app config.

from the sounds of it though, the OP is trying to edit a dll.config which is not ever going to work. those settings need to be in the exe.config in order to be used.

toraj58
January 21st, 2009, 06:40 AM
what do you mean by app config; you mean that user config and app config are two seperated files?

MadHatter
January 21st, 2009, 07:48 AM
myapp.exe.config and the \users\me\AppData\local\MySoftware\MyApplication_key\1.0.0.0\user.config are indeed two different files. the latter is where user settings are stored (once you call save on your settings). If the user.config isn't present it will use the setting from the myapp.exe.config file. if the assembly requesting app settings is in a dll, the app settings have to be in the myapp.exe.config (the myapp.exe where the dll is loaded) in order for the settings to use the values. By default the code generated in the resource editor sets a default value (as an attribute I believe) in the settings class. This default value is whatever was set in the designer. in the OP's case, since there were no settings in the app.exe.config file that it needed, it used the default values entered in the resource editor. If there were settings in the app.exe.config file it would use them. If there were a user.config file in the users local storage location, it would use that.

shers
January 22nd, 2009, 12:29 AM
I used User and the value is stored in the config file. I changed in the cofig file, but it does not read when I run the app.

To be clear, my intention is to create a dll which works with a COM application. The dll is placed in the network, where the cofig file should also be placed. The administrator should be able to edit the xml file in notepad to change the path of the initial directory, which will happen once in a while.
If it is not possible with Settings, what could be the other option?

MadHatter
January 22nd, 2009, 01:47 AM
you will have to hand code a configuration file yourself.

Create a class that wraps a local app config lookup (hint, you can check for an app config existance in the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).

If the app config exists, load it up in an xml doc,
else create one (using the Assembly.GetExecutingAssembly().Location path, and append it's value w/ a .config to get the path to a would be config file).

If the app config exists, check it to see if your custom config section is in the app config. If it exists, load it up,
else, create the config section (remember to add an IgnoreSectionHandler in the app config document for your custom config section).

load or save whatever settings you want from your custom configuration class, and remember to write the new sections to the config file, so that it can be persisted when you load it next.


for a good example on how this would work, take a look at log4net (http://logging.apache.org/log4net/) at the way they handle their config sections.

toraj58
January 23rd, 2009, 04:05 PM
sher what about forgeting XML like config file and writing it to a simple text file because your configuration is not so complicated.

shers
January 25th, 2009, 10:01 PM
sher what about forgeting XML like config file and writing it to a simple text file because your configuration is not so complicated.

I too prefer something simple rather than going for complicated coding. What do you suggest about a .ini file?

toraj58
January 26th, 2009, 08:11 AM
it think it is ok.

cjard
January 26th, 2009, 10:10 PM
my project name is bm. It is a dll project.

that is your "problem"

dlls cannot have xml settings files. The settings you create are compiled into the dll, which is why everything works on another machine without the config file.
Think about the logic of this for a second.. Dlls are supposed to encapsulate some common functionality. Ergo, why would they have settings? Everything they need should be provided at the time of calling. You'd be annoyed if you had to create an ini file specifying the source and destiantion path every time you used the file.copy command in the .net core dll wouldn't you?

shers
January 26th, 2009, 11:28 PM
that is your "problem"

dlls cannot have xml settings files. The settings you create are compiled into the dll, which is why everything works on another machine without the config file.
Think about the logic of this for a second.. Dlls are supposed to encapsulate some common functionality. Ergo, why would they have settings? Everything they need should be provided at the time of calling.

You must be right.

Please see this link and read the section
Changing the Value of a Setting Between Application Sessions
http://msdn.microsoft.com/en-us/library/aa730869(vs.80).aspx

This is for exe.


You'd be annoyed if you had to create an ini file specifying the source and destiantion path every time you used the file.copy command in the .net core dll wouldn't you?

So what's the solution?

cjard
January 27th, 2009, 03:14 AM
I gave you it already.. Rethink your program so your DLL doesnt need settings! It's the program that should have settings.. DLLs should be suitably generic so that they do not need them, no?

Tell us more about your DLL and what setting youre trying to make. You said something about "default directory the user picks"? In what context do you mean?

shers
January 27th, 2009, 10:45 PM
The app is pointed to a certain location in the shared network, as there are some images in some organised folders. The main purpose of this app is to display these images to the users like a library. This is a COM application where it is linked to AutoCAD. This dll will be loaded into AutoCAD and executed using commands. The network share drive I mentioned earlier may be subject to change in future. So if there is a config or ini file, then the person in charge can change the location without tampering the code.