CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Config.xml

  1. #1
    Join Date
    Apr 2004
    Posts
    265

    Config.xml

    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

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Config.xml

    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:

    Code:
            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;
            }
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Apr 2004
    Posts
    265

    Re: Config.xml

    I have done the same. But it doesn't work.

  4. #4
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Config.xml

    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.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  5. #5
    Join Date
    Apr 2004
    Posts
    265

    Re: Config.xml

    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>

  6. #6
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Config.xml

    the XML format is correct, you shoud use following code to read and write the path:

    Code:
    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.
    Last edited by toraj58; January 21st, 2009 at 03:15 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  7. #7
    Join Date
    Apr 2004
    Posts
    265

    Re: Config.xml

    Quote Originally Posted by toraj58 View Post
    the XML format is correct, you shoud use following code to read and write the path:

    Code:
    Properties.Settings.Default.ParentPath;
    That is how I have coded. Here it is:

    Code:
    private string parPath = Properties.Settings.Default.ParentPath.ToString();
    Quote Originally Posted by toraj58 View Post
    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

  8. #8
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Config.xml

    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.
    Last edited by MadHatter; January 21st, 2009 at 07:26 AM.

  9. #9
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Config.xml

    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.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  10. #10
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Config.xml

    Quote Originally Posted by MadHatter
    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)
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  11. #11
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Config.xml

    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.

  12. #12
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Config.xml

    what do you mean by app config; you mean that user config and app config are two seperated files?
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  13. #13
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Config.xml

    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.

  14. #14
    Join Date
    Apr 2004
    Posts
    265

    Re: Config.xml

    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?

  15. #15
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Config.xml

    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 at the way they handle their config sections.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured