标签:
一、文件夹的操作 
private void button1_Click(object sender, EventArgs e)
        {
            //文件夹操作
            ////新建文件夹
            //Directory.CreateDirectory(@"F:\zhoubinn");
            //DirectoryInfo dir = new DirectoryInfo(@"F:\zhoubin1");
            //dir.Create();
        }
        private void button2_Click(object sender, EventArgs e)
        {//删除文件夹
            //Directory.Delete(@"F:\zhoubinn");
            //DirectoryInfo aa = new DirectoryInfo(@"F:\zhoubin1");
            //aa.Create();
        }
        private void button4_Click(object sender, EventArgs e)
        {//   判断文件夹是否存在
            //bool bo = Directory.Exists(@"F:\zhoubinn");
            //if (bo == true)
            //{
            //    MessageBox.Show("文件夹存在");
            //}
            //else
            //{
            //    MessageBox.Show("文件不不存在");
            //}
        }
        private void button3_Click(object sender, EventArgs e)
        {  // 获取某个文件夹内子文件名
            //string[] aa = Directory.GetFiles(@"F:\zhoubin");
            ////listBox1.Items.Clear();
            //foreach (string a in aa)
            //{///显示在listbox里面
            //    listBox1.Items.Add(a);
            //}
            //DirectoryInfo name = new DirectoryInfo(@"F:\zhoubin");
            //FileInfo[] af = name.GetFiles();
            //// listBox1.Items.Clear();
            //foreach (FileInfo a in af)
            //{
            //    listBox1.Items.Add(a.FullName);
            //}
        }
        private void button5_Click(object sender, EventArgs e)
        {   // 获取某个文件中子文件中的名字
            //string[] name = Directory.GetDirectories(@"F:\zhoubin");
            //listBox1.Items.Clear();
            //foreach (string a in name)
            //{
            //    listBox1.Items.Add(a);
            //}
            //DirectoryInfo ac = new DirectoryInfo(@"F:zhoubin");
            //DirectoryInfo[] s = ac.GetDirectories();
            //foreach (DirectoryInfo a in s)
            //{
            //    listBox1.Items.Add(a);
            //}
        }
        private void button8_Click(object sender, EventArgs e)
        {
            //获取文件夹的创建时间,最后一次的访问时间 最后一次写入时间
            //string r = @"F:\zhoubin";
            //DateTime createtime = Directory.GetCreationTime(r);
            //DateTime fangwen = Directory.GetLastAccessTime(r);
            //DateTime xieru = Directory.GetLastWriteTime(r);
            //listBox1.Items.Add(createtime);
            //listBox1.Items.Add(fangwen);
            //listBox1.Items.Add(xieru);
            DirectoryInfo afg = new DirectoryInfo(@"F:\zhoubin1");
            DateTime a = afg.CreationTime;
            DateTime b = afg.LastAccessTime;
            DateTime c = afg.LastWriteTime;
            listBox1.Items.Add(afg.LastWriteTime);
            listBox1.Items.Add(b);
            listBox1.Items.Add(c);
}
        private void button9_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }
        private void button6_Click(object sender, EventArgs e)
        {
            string path = @"F:\zhoubin1";
            Directory.SetLastWriteTime(path,new DateTime(2030,3,4));
            Directory.SetLastAccessTime(path,new DateTime(2040,5,6));
            Directory.SetCreationTime(path,new DateTime(1984,4,5));
        }
        private void button7_Click(object sender, EventArgs e)
        {
            string a = @"F:\zhoubin\123";
            string b = @"F:\zhoubin1\";
            Directory.Move(a,b);
        }
    }
二、 读写文件的操作
   private void button1_Click(object sender, EventArgs e)
        {
            // 读取文件内容
            String path = @"F:\wenjianduqu\读取2.doc";
            //实例化流文件
            FileStream stream = new FileStream(path, FileMode.Open);
            //将文件转换为二进制字符;
            byte[] nr = new byte[stream.Length];
            //stream.Write();
            //读取流文件
            stream.Read(nr, 0, nr.Length);
            //关闭流文件
            stream.Close();
            //将读取的流文件从二进制转化为字符串
            //  String s = System.Text.Encoding.Default.GetString(nr);
            string s = System.Text.Encoding.UTF32.GetString(nr);
            textBox1.Text = s;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //  读取文件内内容方法二
            string path = @"F:\duquwenjian\读取2.txt";
            FileStream stream = new FileStream(path, FileMode.Open);
            StreamReader reader = new StreamReader(stream, Encoding.Default);
            string ss = reader.ReadToEnd();
            reader.Close();
            stream.Close();
            textBox1.Text = ss;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            //读取文件内容方法三
            string path = @"D:\duqu.txt";
            StreamReader reader = new StreamReader(path, Encoding.Default);
            string a = reader.ReadToEnd();
            textBox1.Text = a;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            //读取文件内容方法4
            DialogResult dia = open.ShowDialog();
            if (dia == System.Windows.Forms.DialogResult.OK)
            {
                StreamReader reader = new StreamReader(open.FileName, Encoding.Default);
                string ssse = reader.ReadToEnd();
                reader.Close();
                textBox1.Text = ssse;
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            // 写入文件内容
            string path = @"F:\wenjianduqu\abc.txt";
            StreamWriter writ = new StreamWriter(path, false, Encoding.Default);
            //fause 将源文件全部替换;ture 在源文件的基础上追加
writ.WriteLine(textBox1.Text);
writ.Close();
}
        private void button6_Click(object sender, EventArgs e)
        {
            //写入内容2
            DialogResult dia = save.ShowDialog();
            if (dia == System.Windows.Forms.DialogResult.OK)
            {
                StreamWriter writ = new StreamWriter(save.FileName, true, Encoding.Default);
writ.WriteLine(textBox1.Text);
                writ.Close();
            }
}
        private void button7_Click(object sender, EventArgs e)
        {
            //写入内容3
            DialogResult dia = save.ShowDialog();
            if (dia == System.Windows.Forms.DialogResult.OK)
            {
                FileStream stream = new FileStream(save.FileName, FileMode.OpenOrCreate);
                StreamWriter weri = new StreamWriter(stream);
                weri.WriteLine(textBox1.Text);
                stream.Close();
                weri.Close();
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            //写入文件方法4
            if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                byte[] nr = System.Text.Encoding.Default.GetBytes(textBox1.Text);
                FileStream strea = new FileStream(save.FileName, FileMode.OpenOrCreate);
                strea.Write(nr, 0, nr.Length);
                strea.Close();
            }
}
标签:
原文地址:http://www.cnblogs.com/woniu-net/p/4679698.html