my senior gave me an assignment in which if app.config is missing (deleted by user, perhaps) then a new config file would be created (of course, with the same configuration as the old one).
So I googled and searched stack regarding this one and I found this with the same situation as I am now so I followed the answer given to the OP in that post. Done and done, build and run. I sent the exe file with the DLLs to my friend to test it and I made sure that MyApp.EXE.config is not included in the folder to test if it would work.
The result was that upon clicking MyApp.exe the first time, we didn't encounter any problems. So my friend clicked a button to open a file dialog and select a file, we encountered a dialog box containing a message regarding "Unhandled exception..." When I read over the details of the message, I saw these:
System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsFormsApplication1.Form1.browsecfg_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
and a bunch of other stuff and there's the JIT debugging as well.
We closed the app and opened it again and now before the form shows up, we got another dialog box about unhandled exception and it contains these details:
>System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section AppSettings. (C:\Users\uidr3024\Desktop\SVT_Tool\Continental.exe.config line 20)
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
--- End of inner exception stack trace ---
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.get_AppSettings()
at WindowsFormsApplication1.Form1.ConfigurationFile()
at WindowsFormsApplication1.Form1.Form1_Load_1(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
This is the code on creating a new config file if it doesn't exist:
public void ConfigurationFile()
{
string path1;
string path2;
if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["cfgPath"]))
{
System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sb.AppendLine("<configuration>");
sb.AppendLine("<system.windows.forms jitDebugging=\"true\"/>");
sb.AppendLine("<runtime>");
//something else written inside runtime
sb.AppendLine("</runtime>");
sb.AppendLine("<AppSettings>");
sb.AppendLine("<add key=\"cfgPath\" value=\"\"/>");
sb.AppendLine("<add key=\"xmlPath\" value=\"\"/>");
sb.AppendLine("</AppSettings>");
sb.AppendLine("</configuration>");
string loc = Assembly.GetEntryAssembly().Location;
System.IO.File.WriteAllText(String.Concat(loc, ".config"));
}
else
{
path1 = config.AppSettings.Settings["cfgPath"].Value.ToString();
textBox1.Text = path1;
path2 = config.AppSettings.Settings["xmlPath"].Value.ToString();
textBox2.Text = path2;
}
}
And this is the original app.config:
<?xml version="1.0"?>
<configuration>
<system.windows.forms jitDebugging="true"/>
<runtime>
//something else written inside runtime
</runtime>
<appSettings>
<add key="cfgPath" value=" "/>
<add key="xmlPath" value=" "/>
</appSettings>
</configuration>
The AppSettings keys cfgPath
and xmlPath
are the paths of the last selected files from the file dialog box and is stored in the config file. The values of the keys would then be written on their respective textboxes (so the user would see what's the last file selected the next user opens the app). But when I checked the config file, it's not stored there and the values remain to be blank and the textboxes are blank as well.
Am I doing all of these right? Or are there any other ways to do this?
Aucun commentaire:
Enregistrer un commentaire