app.config issue
-
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
-
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
-
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
-
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()); }
Hi Alan! Your code works ok in a Windows forms application. But for a console application, how to handle? regards, George
-
You could try to subscribe to the
System.AppDomain.UnhandledException
event, see if you can intercept the app.config error.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
-
Hi Alan! Your code works ok in a Windows forms application. But for a console application, how to handle? regards, George
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; } }
-
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; } }
Hi Alan, I am writing a DLL, could I handle such issue inside the DLL? regards, George
-
Hi Alan, I am writing a DLL, could I handle such issue inside the DLL? regards, George
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.
-
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.
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