
建立一个WinForm应用程序 添加MenuStrip控件 填写两个功能 读取 和 导出数据
用了两个DataSet控件和对话框 打开(OpenFilesDialog控件) 和 保存(SaveFilesDialog控件)
读取 private void 读取ToolStripMenuItem_Click(object sender EventArgs e) { if (opFileDlg ShowDialog() == DialogResult OK) { if(opFileDlg OpenFile()!=null) { oXML ReadXml (@opFileDlg FileName ) foreach (DataRow oRow in oXML Tables [ user ] Rows) { DataRow newRow = dsXML Tables[ user ] NewRow() newRow [ 序号 ] = oRow [ 序号 ] newRow[ 标题 ] = oRow[ 标题 ] newRow[ 网址 ] = oRow[ 网址 ] newRow[ 用户名 ] = oRow[ 用户名 ] newRow[ 密码 ] = oRow[ 密码 ] newRow[ 时间 ] = oRow[ 时间 ] newRow[ 备注 ] = oRow[ 备注 ] dsXML Tables [ user ] Rows Add(newRow) } int n = dsXML Tables [ user ] Rows Count for(int i= i<ni++) { dsXML Tables [ user ] Rows [i][ 序号 ]=i+ } dsXML WriteXml(@ user xml ) this Visible = true MessageBox Show( 数据导入成功! 成功 ) } } else { this Visible = true } } 导出
private void 导出ToolStripMenuItem_Click(object sender EventArgs e) { if (svFileDlg ShowDialog() == DialogResult OK) { dsXML WriteXml(@svFileDlg FileName) this Visible = true MessageBox Show( 数据导出成功! 成功 ) } else { this Visible = true } }
lishixinzhi/Article/program/ASP/201311/21826
在开发Web项目的时候,会有一个配置文件Web.config,用来存放一些全局
的变量,如连接数据库用的字符串。相应的,在开发winform程序时,也有一个配置文件,它就是App.config,这个文件的作用与
Web.config大致相同,也可以用来存放程序所用的全局变量及Value值。
来看一个app.config文件的例子:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!--图片存放路径-->
<add key="ImgPath" value="D:\img\" />
</appSettings>
</configuration>
可以看出,app.config和web.config一样,嗯,它也是一个XML文件。那怎么对这个文件中的元素进行读取 *** 作呢?很简单,来看代码:
string strPath = System.Configuration.ConfigurationSettings.AppSettings["ImgPath"].ToString()
这样就可以把app.config文件中ImgPath这个元素的Value值读取出来了。那怎么改写元素的值呢?如果你认为像读那样的去写,像这样的代码:
System.Configuration.ConfigurationSettings.AppSettings["ImgPath"] = @"E:\img\"//这样写是没用的
在对app.config文件的元素Value值进行修改 *** 作时,只能把app.config文件当作一个普通的XML文件来对待,利用
System.Xml.XmlDocument类把这个app.config文件读到内存中,并通过System.Xml.XmlNode类找到
appSettings节点,通过System.Xml.XmlElement类找到节点下的某个元素,利用SetAttribute方法来修改这个元素
的值后,最后再将app.config文件保存到原的目录中,这样,才算完成了对一个元素Value值的修改 *** 作。下面这个方法可完成对
app.config文件appSettings节点下任意一个元素进行修改,当然,你也可能修改这个方法,达到修改任意节点,任意元素的Value值。
public static void SetValue(string AppKey, string AppValue)
{
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument()
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config")
System.Xml.XmlNode xNode
System.Xml.XmlElement xElem1
System.Xml.XmlElement xElem2
xNode = xDoc.SelectSingleNode("//appSettings")
xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']")
if (xElem1 != null) xElem1.SetAttribute("value", AppValue)
else
{
xElem2 = xDoc.CreateElement("add")
xElem2.SetAttribute("key", AppKey)
xElem2.SetAttribute("value", AppValue)
xNode.AppendChild(xElem2)
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config")
}
注意这个方法中if条件else下的语句,当在文件中没有找到给定的元素时,方法会创建这个元素。
C#读写txt文件的两种方法:1.添加命名空间
System.IO
System.Text
2.文件的读取
(1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出。
byte[] byData = new byte[100]
char[] charData = new char[1000]
public void Read()
{
try
{
FileStream file = new FileStream("E:\\test.txt", FileMode.Open)
file.Seek(0, SeekOrigin.Begin)
file.Read(byData, 0, 100)//byData传进来的字节数组,用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.
Decoder d = Encoding.Default.GetDecoder()
d.GetChars(byData, 0, byData.Length, charData, 0)
Console.WriteLine(charData)
file.Close()
}
catch (IOException e)
{
Console.WriteLine(e.ToString())
}
}
(2).使用StreamReader读取文件,然后一行一行的输出。
public void Read(string path)
{
StreamReader sr = new StreamReader(path,Encoding.Default)
String line
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line.ToString())
}
}
3.文件的写入
(1).使用FileStream类创建文件,然后将数据写入到文件里。
public void Write()
{
FileStream fs = new FileStream("E:\\ak.txt", FileMode.Create)
//获得字节数组
byte[] data = System.Text.Encoding.Default.GetBytes("Hello World!")
//开始写入
fs.Write(data, 0, data.Length)
//清空缓冲区、关闭流
fs.Flush()
fs.Close()
}
(2).使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。
public void Write(string path)
{
FileStream fs = new FileStream(path, FileMode.Create)
StreamWriter sw = new StreamWriter(fs)
//开始写入
sw.Write("Hello World!!!!")
//清空缓冲区
sw.Flush()
//关闭流
sw.Close()
fs.Close()
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)