在WPF中读写config配置文件,很简单。
1. 在你的工程中,添加app.config文件。文件的内容绝如默认为:
[csharp] view plain copy
<中裤?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
2.如果你想给程序配置一些参数,就在<configuration>标签中添加<appSettings>.例如:[html] view plain copy
<?xml version="1.0"卖宏简 encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Path" value="SFSFSDSDS"/>
<add key="NAME" value="FUCk"/>
</appSettings>
</configuration>
3.然后你在程序里需要的地方读写它就可以了。先引用[csharp] view plain copy
using System.Configuration
4.读:[csharp] view plain copy
string str = ConfigurationManager.AppSettings["Path"]
5.写:[csharp] view plain copy
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
cfa.AppSettings.Settings["NAME"].Value = "WANGLICHAO"
cfa.Save()
6.收工!private void CheckBox_是否保存为文本文件_Checked(object sender, RoutedEventArgs e){
string fileName = string.Empty
if (string.IsNullOrEmpty(this.TextBox_文件位置.Text))
{
var dialog = new SaveFileDialog { DefaultExt = "txt"旁燃, AddExtension = true, FileName = DateTime.Now.ToString("yyyyMMdd_HHmmss", DateTimeFormatInfo.CurrentInfo) + "发票" }
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
System.Windows.MessageBox.Show("请选择要保存的文运败虚件枯腔名称")
return
}
fileName = dialog.FileName
}
else
{
fileName = TextBox_文件位置.Text
}
using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
{
writer.Write(TextBox_发票.Text)
}
}
private void Button_选择_Click(object sender, RoutedEventArgs e)
{
ClearText()
var dialog = new OpenFileDialog { Multiselect = false, Filter = "TxtFiles|*.txt" }
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.TextBox_文件位置.Text = dialog.FileName
using (var stream = new StreamReader(dialog.FileName, Encoding.Default))
{
TextBox_发票.Text = stream.ReadToEnd()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)