码迷,mamicode.com
首页 > 其他好文 > 详细

文件IO

时间:2014-06-20 15:58:13      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   http   

文件IO

bubuko.com,布布扣
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

bubuko.com,布布扣
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

 

文件IO,布布扣,bubuko.com

文件IO

标签:des   style   class   blog   code   http   

原文地址:http://www.cnblogs.com/liuhao2050/p/3796705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!