I'm having an issue trying to work with a config file,
I've read a few posts here and elsewhere
but I can't solve problem in work,
In my question here, I have added the Configuration.
Declared in the document
string ab123 = ConfigurationManager.AppSettings["ab123"];
But in the side , I show error is
" win32 Exception was unhandled - System can not find the file specified"
System.Diagnostics.Process.Start(ConfigurationManager.AppSettings["ab123"]);
When I run this code, ab123
value always null !!!!!
I sure path is normal
How can I fix it?
Answer
It appears from your xml config files that you are really trying to use User Settings rather than Application setting and that you have mixed some of the ideas up. I think a more correct version of a config might be:
D:\ab123\Source\ab123.c
The only significant difference is the way you define settings. For example I changed it to:
D:\ab123\Source\ab123.c
You can create more settings just like this using a different name
The client code is a bit different as it has to find the userSettings, find the program property settings and then query for the key (like ab123
). I have added some trivial error handling but you need to deal with the errors yourself. I simply return on error for code simplification. The code has inline comments to help figure out what is going on.
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Retrieve the userSettings gorup
ConfigurationSectionGroup group = config.SectionGroups[@"userSettings"];
if (group == null) return;
// Get the program settings
ClientSettingsSection clientSection = group.Sections["CA.Properties.Settings"] as ClientSettingsSection;
if (clientSection == null) return;
// This retrieves the value associated with the key
string sFileName = clientSection.Settings.Get("ab123").Value.ValueXml.InnerText;
// Check if ab123 has a value and the file exists
if (!string.IsNullOrEmpty(sFileName) && System.IO.File.Exists(sFileName))
{
using (StreamReader sr = new StreamReader(sFileName))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
System.Diagnostics.Debug.WriteLine(line);
}
}
}
}
}
}
If you are using Settings.settings to create and delete settings then the code can be simplified to this since Visual Studio will create bindings for your settings object that be accessed at design time and runtime. For information on using Settings.settings through the Visual Studio IDE please see this article. If for some reason the code below doesn't work you can use the code above:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string sFileName = Properties.Settings.Default.ab123;
// Check if ab123 has a value and the file exists
if (!string.IsNullOrEmpty(sFileName) && System.IO.File.Exists(sFileName))
{
using (StreamReader sr = new StreamReader(sFileName))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
System.Diagnostics.Debug.WriteLine(line);
}
}
}
}
}
}
No comments:
Post a Comment