您的位置:首页 > 编程语言 > C#

C# 文件及其文件夹基础操作

2014-08-17 19:02 330 查看
关于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]);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件系统 c#