一、如何读写文件
1、创建一个文件流
2、创建阅读器或者写入器
3、执行读写操作
4、关闭阅读器或者写入器
5、关闭文件流
示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace TextReader
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class TextReader : Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public TextReader()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void btnWrite_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string path = txtFilePath.Text;
string content = txtContent.Text;
if (String.IsNullOrEmpty(path) == true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("文件路径不能为空");
return;
}
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//创建文件流
FileStream myFs = new FileStream(path, FileMode.CreateNew);
//创建写入器
StreamWriter mySw = new StreamWriter(myFs);
//将录入的内容写入文件
mySw.Write(content);
//关闭写入器
mySw.Close();
//关闭文件流
myFs.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////直接写入方式
//StreamWriter mySw = new StreamWriter(path);
//mySw.Write(content);
//mySw.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MessageBox.Show("写入成功");
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(ex.Message.ToString());
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void btnRead_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string path = txtFilePath.Text;
string content;
if (String.IsNullOrEmpty(path) == true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("文件路径不能为空");
return;
}
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//创建文件流
FileStream myfs = new FileStream(path, FileMode.Open);
//创建读取器
StreamReader mySr = new StreamReader(myfs);
//读取文件所有内容
content = mySr.ReadToEnd();
txtContent.Text = content;
//关闭读取器
mySr.Close();
//关闭文件流
myfs.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////直接读取方式
//StreamReader mySr = new StreamReader(path);
//content = mySr.ReadToEnd();
//txtContent.Text = content;
//mySr.Close();
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
文件流:
Create用指定的名称新建一个文件
CreateNew新建一个文件
Open打开一个文件
OpenOrCreate如果文件不存在,则用指定的名称新建一个文件并打开它
FileMode属于枚举型
注意:读取文件数据时,其FileMode应该设置为FileMode.Open
二、File类的方法:
Exists(string path)用于检查指定文件是否存在,该方法返回一个布尔值
Copy(string SourceFilePath,string DestinationFilePath)
Move(string sourceFileName,string destFileName)
Delete(string path)
三、Directory类的方法:
Exists(string path)
Move(string sourceDirName,string destDirName)
Delete(string,bool)
四、XML的解析:
操作XML的对象属性和方法
1、XmlDocument对象:DocumentElement属性 获取根节点
ChildNodes属性 获取所有子节点
Load()方法 读取整个XML的结构
2、XmlNode对象:InnerText属性 当前节点的值
Name属性 当前节点的名字
ChildNodes属性 当前节点的所有子节点
示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public bool FetchArticles()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string filePath = "temp.rss";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.articles == null)
this.articles = new Dictionary<string, Article>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Articles.Clear();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
WebClient myClient = new WebClient();
myClient.DownloadFile(Url, filePath);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
XmlDocument myXml = new XmlDocument();
myXml.Load(filePath);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//定位 channel 节点
XmlNode channel = myXml.DocumentElement.FirstChild; //channel node
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//定位 item 节点
foreach (XmlNode node in channel.ChildNodes)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (node.Name == "item")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Article atcl = new Article();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (XmlNode subNode in node.ChildNodes)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (subNode.Name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "title":
atcl.Title = subNode.InnerText;
break;
case "link":
atcl.Url = subNode.InnerText;
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Articles.Add(atcl.Title, atcl);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Clicks++;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return true;
}
catch (Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(e.ToString());
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}