Why is this C# load form not working? [closed]
I am trying to get a value from a config file when the form loads it puts it in a Textbox. I am using Program.cs to get the value from the config file. Everything looks setup correctly but when I run the form no value is there.
public form()
{
InitializeComponent();
string NewValue = Program.Value;
Textbox.Text = NewValue;
}
Program.cs:
public static string Value = "";
switch (element.ChildNodes[i].Name)
{
case "FileInfo":
{
for (int j = 0; j < childNodeList.Count; j++)
{
switch (childNodeList[j].Name)
{
case "Value":
{
Program.Value = childNodeList[j].InnerText;
break;
}
}
}
.........
}
---------
}
Config:
<config>
<FileInfo>
<Value>1234</Value>
</FileInfo>
</config>
I'm assuming that you are using at least .net 2.0
- Add reference to System.Configuration to your project
Make sure that your app.config looks properly:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="myKey" value="myValue"/> </appSettings> </configuration>
Change you code to use
ConfigurationManager
to retrieve the value:using System.Configuration; using System.Windows.Forms; namespace SO6065319 { public partial class Form1 : Form { public Form1() { InitializeComponent(); textBox1.Text = ConfigurationManager.AppSettings["myKey"]; } } }
Here textBox1 is the name of the text box on the form. You can see now "myValue" in your textbox.
Read about ConfigurationManager.
精彩评论