Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. app.config issue

app.config issue

Scheduled Pinned Locked Moved C#
helpxmltutorialquestion
9 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, If the app.config format is wrong, for example, not a correct format XML file, application will fail from loading. Are there any ways to let me know such issue -- for example, receiving some events (so that I could write file log and event log to record this issue) if app.config loads error because of a mal-formatted XML file? thanks in advance, George

    M A 2 Replies Last reply
    0
    • G George_George

      Hello everyone, If the app.config format is wrong, for example, not a correct format XML file, application will fail from loading. Are there any ways to let me know such issue -- for example, receiving some events (so that I could write file log and event log to record this issue) if app.config loads error because of a mal-formatted XML file? thanks in advance, George

      M Offline
      M Offline
      Mirko1980
      wrote on last edited by
      #2

      You could try to subscribe to the System.AppDomain.UnhandledException event, see if you can intercept the app.config error.

      G 1 Reply Last reply
      0
      • G George_George

        Hello everyone, If the app.config format is wrong, for example, not a correct format XML file, application will fail from loading. Are there any ways to let me know such issue -- for example, receiving some events (so that I could write file log and event log to record this issue) if app.config loads error because of a mal-formatted XML file? thanks in advance, George

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        Hi, Code like this will catch the exception which occurs when the form is created. Alan.

          try {
            CsTermForm mainForm = new CsTermForm();
            Application.Run(mainForm);
          } catch (System.Configuration.ConfigurationException ce) {
            MessageBox.Show(ce.ToString());
          }
        
        G 1 Reply Last reply
        0
        • A Alan N

          Hi, Code like this will catch the exception which occurs when the form is created. Alan.

            try {
              CsTermForm mainForm = new CsTermForm();
              Application.Run(mainForm);
            } catch (System.Configuration.ConfigurationException ce) {
              MessageBox.Show(ce.ToString());
            }
          
          G Offline
          G Offline
          George_George
          wrote on last edited by
          #4

          Hi Alan! Your code works ok in a Windows forms application. But for a console application, how to handle? regards, George

          A 1 Reply Last reply
          0
          • M Mirko1980

            You could try to subscribe to the System.AppDomain.UnhandledException event, see if you can intercept the app.config error.

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            Thanks Mirko1980! My code and app.config looks like this, but no exception is thrown. Any ideas?

            class Program
            {
                public static void MyEventHandler(object sender, EventArgs e)
                {
                    return;
                }
            
                static void Main(string\[\] args)
                {
                    AppDomain currentDomain = AppDomain.CurrentDomain;
                    currentDomain.UnhandledException += MyEventHandler;
            
                    return;
                }
            }
            

            <?xml version="1.0" encoding="utf-8" ?>
            <configuration>
            <configuration>

            regards, George

            1 Reply Last reply
            0
            • G George_George

              Hi Alan! Your code works ok in a Windows forms application. But for a console application, how to handle? regards, George

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #6

              Hi, Pretty much the same really. You just have to wrap any code that may access the settings in an exception block. I've just trashed one of my config files and the following code trapped the exception.

              static Int32 Main(string\[\] args) {
                try {
                  App app = new App();
                  Int32 exitcode = app.Run();
                  return exitcode;
                } catch (System.Configuration.ConfigurationException ce) {
                  Console.WriteLine(ce);
                  return 1;
                }
              }
              
              G 1 Reply Last reply
              0
              • A Alan N

                Hi, Pretty much the same really. You just have to wrap any code that may access the settings in an exception block. I've just trashed one of my config files and the following code trapped the exception.

                static Int32 Main(string\[\] args) {
                  try {
                    App app = new App();
                    Int32 exitcode = app.Run();
                    return exitcode;
                  } catch (System.Configuration.ConfigurationException ce) {
                    Console.WriteLine(ce);
                    return 1;
                  }
                }
                
                G Offline
                G Offline
                George_George
                wrote on last edited by
                #7

                Hi Alan, I am writing a DLL, could I handle such issue inside the DLL? regards, George

                A 1 Reply Last reply
                0
                • G George_George

                  Hi Alan, I am writing a DLL, could I handle such issue inside the DLL? regards, George

                  A Offline
                  A Offline
                  Alan N
                  wrote on last edited by
                  #8

                  George,

                  George_George wrote:

                  could I handle such issue inside the DLL?

                  That I don't know. I've only ever used a top level handler designed to just stop the application dead in it's tracks if the config file is corrupted. If the exception occurs within the dll it will of course be passed up the call stack until a handler is found. One thing to note is that changes to the dll's user scoped configuration settings are stored in the exe's user.config file so I suspect the best place to trap any exceptions caused by malformed xml is very early in the execution of the application. Anything more precise than that is beyond my current level of knowledge. I had a quick look at the System.Configuration namespace and decided I wouldn't go back there again unless I absolutely had to! Alan.

                  G 1 Reply Last reply
                  0
                  • A Alan N

                    George,

                    George_George wrote:

                    could I handle such issue inside the DLL?

                    That I don't know. I've only ever used a top level handler designed to just stop the application dead in it's tracks if the config file is corrupted. If the exception occurs within the dll it will of course be passed up the call stack until a handler is found. One thing to note is that changes to the dll's user scoped configuration settings are stored in the exe's user.config file so I suspect the best place to trap any exceptions caused by malformed xml is very early in the execution of the application. Anything more precise than that is beyond my current level of knowledge. I had a quick look at the System.Configuration namespace and decided I wouldn't go back there again unless I absolutely had to! Alan.

                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #9

                    Thanks Alan, 1. I can put the exception handling to EXE level. But it is still different from your posted solution before -- you use a starter EXE to track the actual configuraiton issues in the inner EXE, correct? 2. So, do you have any ideas to let the EXE itself handle the app.config issue? 3. If you do not suggest to handle such issue, what is your point and any advice to let end user know such issues (because of configuration file format error)? regards, George

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups