C# 文件及其文件夹基础操作
来源:互联网 发布:日本人 二战 知乎 编辑:程序博客网 时间:2024/06/11 20:37
关于C#中文件及其文件夹的一些常用的方法与属性的应用:
winform界面:
代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace 练习{ public partial class 文件处理 : Form { string[] filepath;//文件路径 string filename; bool fileanddir; public 文件处理() { InitializeComponent(); } private void 文件处理_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;//复制 else e.Effect = DragDropEffects.None; } private void 文件处理_DragDrop(object sender, DragEventArgs e)//支持拖放,这里主要是操作第一个文件filepath[0] { filepath = e.Data.GetData(DataFormats.FileDrop,false) as string[]; textBox1.Text = filepath[0]; if(File.Exists(filepath[0])) { MessageBox.Show("文件"); fileanddir = false; } if(Directory.Exists(filepath[0])) { MessageBox.Show("文件夹"); fileanddir = true; } } private void button3_Click(object sender, EventArgs e)//文件信息获取 { if (!fileanddir) { FileInfo file = new FileInfo(filepath[0]); string[] name = filepath[0].Split('\\'); label5.Text = (filename = name[name.Length - 1]).Split('.')[0];//还是利用字符串的处理更好 label6.Text = file.CreationTime.ToString();//获取文件的创建时间 label7.Text = file.DirectoryName as string;//获取文件的父目录 label8.Text = file.FullName.ToString();//获取完整的目录 label12.Text = file.Attributes.ToString();//获取文件的属性 } else { DirectoryInfo dir = new DirectoryInfo(filepath[0]); string[] name = filepath[0].Split('\\'); label15.Text = filepath[0].Split('\\')[name.Length - 1];//获取文件夹的名称 label16.Text = dir.CreationTimeUtc.ToString();//获取文件夹的创建时间 label19.Text = Directory.GetLastAccessTime(filepath[0]).ToString();//获取上一次访问文件夹的时间 label21.Text = File.GetAttributes(filepath[0]).ToString();//获取文件夹属性 foreach(FileSystemInfo file in dir.GetFileSystemInfos())//获取当前目录的子目录和文件 { if(file is FileInfo) { FileInfo _file = file as FileInfo; textBox3.Text += _file.Name + "\r\n"; } if(file is DirectoryInfo) { DirectoryInfo _dir = file as DirectoryInfo; textBox3.Text += _dir.Name + "\r\n"; } } } } private void button2_Click(object sender, EventArgs e)//文件复制按钮,文件的覆盖需要两个步骤:1.在复制的目录创建一个文件,2.再文件覆盖到那个文件 { if (!fileanddir) { string copypath = textBox2.Text + "\\" + filename; FileInfo file = new FileInfo(copypath);//创建该文件,解决找不到目标路径 try { using (Stream f = file.Create()) { f.Close();//主要解决文件的线程被占用 } } catch { } File.Copy(filepath[0], copypath, true);//复制覆盖原来的文件 } else//复制整个文件夹需要用到递归 { MessageBox.Show("递归复制文件夹"); } } private void button1_Click(object sender, EventArgs e) { if(radioButton1.Checked == true) setattributes(1); if(radioButton2.Checked == true) setattributes(2); } public void setattributes(int num)//修改文件或文件夹属性 { if (num == 1) File.SetAttributes(filepath[0], FileAttributes.ReadOnly);//支持文件夹进行属性修改 else File.SetAttributes(filepath[0],FileAttributes.Hidden); } private void button4_Click(object sender, EventArgs e)//文件或文件夹的移动 { if (!fileanddir) File.Move(filepath[0], textBox4.Text); else Directory.Move(filepath[0],textBox4.Text); } private void button5_Click(object sender, EventArgs e)//文件搜索 { if (radioButton3.Checked == true) { diguisousuo(@"C:\"); } else if (radioButton4.Checked == true) { diguisousuo(@"D:\"); } else if (radioButton5.Checked == true) { diguisousuo(@"E:\"); } else MessageBox.Show("请选择在哪个个盘进行搜索","警告"); } private void diguisousuo(string path) { string[] allfile = Directory.GetFileSystemEntries(path, textBox5.Text);//返回已搜索的文件或目录字符串数组,该方法只能搜索当前文件夹下的文件或目录 if (allfile.Length == 0) { MessageBox.Show("没有找到该文件或文件夹"); return; } textBox1.Text += "搜索到的文件或目录:\r\n"; foreach( string filename in allfile) { textBox1.Text += filename + "\r\n"; } } private void button6_Click(object sender, EventArgs e)//删除文件或目录 { if (!fileanddir) File.Delete(filepath[0]); if (fileanddir) Directory.Delete(filepath[0]); } }}
0 0
- C# 文件及其文件夹基础操作
- C#文件/文件夹操作
- C#文件、文件夹操作
- C# 操作文件夹及文件
- C# 操作文件夹及文件
- C# 操作文件夹及文件
- C# 操作文件夹及文件
- C# 文件,文件夹的操作
- C#操作文件以及文件夹
- C#文件和文件夹操作
- C#常用 文件、文件夹 操作
- C#文件与文件夹操作
- C#操作文件及文件夹
- java--基础---复制文件夹及其中的文件
- C# 遍历文件夹及其子文件夹来查找某个文件
- C# 文件操作(文件夹 及 文件)
- C# 文件操作(文件夹 及 文件) .
- C#读取文件夹中的文件操作浅析
- 允许重复选取的组合数
- 有关提示vector iterator not dereferencable的问题
- 缩写记录
- LeetCode-Remove Element
- hdu 1232畅通工程
- C# 文件及其文件夹基础操作
- 正式生产环境下hadoop集群的DNS+NFS+ssh免密码登陆配置
- 安卓之wifi搜索周围wifi
- Map日常--Map排序,遍历Map
- 011初识css 类选择器 id选择器 html选择器
- PAT_B_字符串-03. 字符串的冒泡排序(20)
- html图片的alt与title:alt:图片加载失败时的提示信息;title:鼠标放上时的提示信息
- 实战UML——给类图加点儿“三层料”
- HDU 3257 Hello World!(字符串找规律——位运算来模拟过程)