文件IO流

文件IO

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 文件IO
{  
    public partial class Form1 : Form
    {   string path=@"d:";
        public Form1()
        {
            InitializeComponent();
        }

        private void butFile_Click(object sender, EventArgs e)
        {   
            ////更改路径字符串的后缀名,不会改变实际路径
            //string newPath = Path.ChangeExtension(@"D:\1.txt", "avi");
            ////合并多个字符路径如果没有\自动加
            //newPath = Path.Combine(@"12.txt", "12.txt");
            //string path = @"G:\BaiduYunDownload\update.bin";
            ////得到文件路径所在目录,如果本身就是目录路径则直接返回
            ////只是操作字符串,不会去寻找文件
            //newPath = Path.GetDirectoryName(path);
            ////得到指定路径的文件名,如果不是文件路径,返回空串
            //newPath = Path.GetExtension(path);
            ////得到路径的文件名(文件名是带扩展名的)
            //newPath = Path.GetFileName(path);
            ////得到没有后缀名的文件名
            //newPath = Path.GetFileNameWithoutExtension(path);
            ////由文件在程序的相对路径得到文件的绝对物理路径
            //newPath = Path.GetFullPath(path );
            ////得到系统的临时目录,使用完由系统清除
            //newPath = Path.GetTempPath();
            ////产生一个随机的系统temp文件,返回文件名;
            //newPath = Path.GetTempFileName();
            
           //创建指定的文件 如果文件存在则覆盖
           // File.Create(@"D:2.txt");

           // 向已有的文件中追加字符串,如果文件不存在则创建文件
           // File.AppendAllText(@"d:\2.txt", "我才是小清新好嘛!");
           // //复制A 到2,并改名为B
           // File.Copy(@"路径1+文件A","路径2+文件B");
           // //删除文件,回收站也没有
           // File.Delete(@"D:\2.txt");
           // //判断文件是否存在
           // File.Exists(@"D:\1.txt");
           // 文件移动
           // File.Move();
           // 打开一个文件读取所有行,关闭文件可以指定编码格式,
           //string filestr= File.ReadAllText(@"d:\1.txt",Encoding.GetEncoding("GB2312"));
           // 如果未知类型可以选择Encoding.default
          //  string[] lines = File.ReadAllLines(@"D:\1.txt",Encoding.Default);

            //MessageBox.Show();
            

        }
    }
}
View Code

TreeView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeView
{
    public partial class Form1 : Form
    {
        string path = @"D:\1";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //获取目录中子文件目录(包括路径)
           string[] dirs= Directory.GetDirectories(path);
          
            foreach(string dir in dirs){
                TreeNode node = new TreeNode();
                node.Text = Path.GetFileName(dir);
                //将全路径保存在node对象的tag中
                node.Tag = dir;
                tvMain.Nodes.Add(node);
            }
        }
       
        /// <summary>
        /// 选中节点之后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvMain_AfterSelect(object sender, TreeViewEventArgs e)
        {   //清空
            lvData.Items.Clear();
            //得到当前选中的节点
           string path= tvMain.SelectedNode.Tag.ToString();
        
            //得到选中的节点所代表的的文件路径

            //将路径下的文件的路径信息读出来
           string[] files = Directory.GetFiles(path);
           ListViewItem item = null;
           FileInfo info = null;
           foreach(string file in files){ 
               info = new FileInfo(file);
               item = new ListViewItem();
              
               item.SubItems.Add(info.Length.ToString());
               item.SubItems.Add(info.CreationTime.ToString());
           item.Text =Path.GetFileName(file);
           lvData.Items.Add(item);
           }
            //加载当前节点的子节点
           string[] dirs = Directory.GetDirectories(path);
           foreach (string str in dirs) {
               TreeNode node = new TreeNode();
               node.Text = Path.GetFileName(str);
               node.Tag = str;
               tvMain.SelectedNode.Nodes.Add(node);
           }
        }

    }
}
View Code

 文件流读取

//1.创建一个文件流对象,指定文件,并指定操作方式
            FileStream stream = new FileStream(@"d:\2.txt",FileMode.Open);
            //2.创建一个byte数组,以供文件流读取数据存入
            byte[] buffer = new byte[1024 * 1024];
            //3.调用文件流的读数据方法,将读出来的数据存入buffer数组中
            stream.Read(buffer,0,buffer.Length);//1M
            //4.转化为字符串
            String content = Encoding.UTF8.GetString(buffer);
            txtContent.Text = content;
            //5.关闭文件(管道)
            stream.Dispose();

文件流写入

            SaveFileDialog sfd = new SaveFileDialog();
           DialogResult dr= sfd.ShowDialog();//弹出文件保存框
           if (dr == System.Windows.Forms.DialogResult.OK) {
               //创建文件流对象
               FileStream stream = new FileStream(sfd.FileName, FileMode.Create);
                //调用文件流写入的方法
               string content=txtContent.Text;
               //转换为byte数组
               byte[] buffer=Encoding.UTF8.GetBytes(content);
              //写入文件
               stream.Write(buffer, 0, buffer.Length);
              //关闭流
               stream.Dispose();

 大文件拷贝时应少量多次,否则不是丢数据就是内存爆了。。

posted on 2014-06-19 15:26  404 NotFound!  阅读(223)  评论(0编辑  收藏  举报

导航