一、如何读写文件

  1、创建一个文件流

  2、创建阅读器或者写入器

  3、执行读写操作

  4、关闭阅读器或者写入器

  5、关闭文件流

示例:


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;

namespace TextReader
{
    
public partial class TextReader : Form
    
{
        
public TextReader()
        
{
            InitializeComponent();
        }


        
private void btnWrite_Click(object sender, EventArgs e)
        
{
            
string path = txtFilePath.Text;
            
string content = txtContent.Text;
            
if (String.IsNullOrEmpty(path) == true)
            
{
                MessageBox.Show(
"文件路径不能为空");
                
return;
            }

            
try
            
{
                
//创建文件流
                FileStream myFs = new FileStream(path, FileMode.CreateNew);
                
//创建写入器
                StreamWriter mySw = new StreamWriter(myFs);
                
//将录入的内容写入文件
                mySw.Write(content);
                
//关闭写入器
                mySw.Close();
                
//关闭文件流
                myFs.Close();

                
////直接写入方式
                //StreamWriter mySw = new StreamWriter(path);
                
//mySw.Write(content);
                
//mySw.Close();

                MessageBox.Show(
"写入成功");
            }

            
catch (Exception ex)
            

                MessageBox.Show(ex.Message.ToString());
            }

        }


        
private void btnRead_Click(object sender, EventArgs e)
        
{
            
string path = txtFilePath.Text;
            
string content;
            
if (String.IsNullOrEmpty(path) == true)
            
{
                MessageBox.Show(
"文件路径不能为空");
                
return;
            }

            
try
            
{
                
//创建文件流
                FileStream myfs = new FileStream(path, FileMode.Open);
                
//创建读取器
                StreamReader mySr = new StreamReader(myfs);
                
//读取文件所有内容
                content = mySr.ReadToEnd();
                txtContent.Text 
= content;
                
//关闭读取器
                mySr.Close();
                
//关闭文件流
                myfs.Close();

                
////直接读取方式
                //StreamReader mySr = new StreamReader(path);
                
//content = mySr.ReadToEnd();
                
//txtContent.Text = content;
                
//mySr.Close();
            }

            
catch (Exception ex)
            

                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属性 当前节点的所有子节点

示例:   

 


 public bool FetchArticles()
        
{
            
string filePath = "temp.rss";

            
try
            
{
                
if (this.articles == null)
                    
this.articles = new Dictionary<string, Article>();

                Articles.Clear();

                WebClient myClient 
= new WebClient();
                myClient.DownloadFile(Url, filePath);

               
 XmlDocument myXml = new XmlDocument();
                myXml.Load(filePath);

                
//定位 channel 节点
                XmlNode channel = myXml.DocumentElement.FirstChild//channel node

                
//定位 item 节点
                foreach (XmlNode node in channel.ChildNodes)
                
{
                    
if (node.Name == "item")
                    
{
                        Article atcl 
= new Article();

                        
foreach (XmlNode subNode in node.ChildNodes)
                        
{
                            
switch (subNode.Name)
                            
{
                                
case "title":
                                    atcl.Title 
= subNode.InnerText;
                                    
break;
                                
case "link":
                                    atcl.Url 
= subNode.InnerText;
                                    
break;
                            }

                        }


                        Articles.Add(atcl.Title, atcl);

                    }

                }


                Clicks
++;

                
return true;
            }

            
catch (Exception e)
            
{
                Console.WriteLine(e.ToString());
                
return false;
            }


        }
posted on 2009-06-10 14:50  8user  阅读(336)  评论(0编辑  收藏  举报