一、文件夹操作
Directory类,DirectoryInfo类.使用using System.IO命名空间
(一)创建文件夹
方法一:
private string path = @"F:\Text\ceshi";
private void Create_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(path);
}
方法二:
private string path = @"F:\Text\ceshi";
private void Create_Click(object sender, EventArgs e)
{
DirectoryInfo CreateDirectory = new DirectoryInfo(path);
CreateDirectory.Create();
}
(二)删除文件夹
方法一:
private string path = @"F:\Text\ceshi";
private void Delete_Click(object sender, EventArgs e)
{
Directory.Delete(path);
}
方法二:
private string path = @"F:\Text\ceshi";
private void Delete_Click(object sender, EventArgs e)
{
DirectoryInfo DeleteDircetory = new DirectoryInfo(path);
DeleteDircetory.Delete();
}
(三)判断文件夹是否存在
方法一:
private string path = @"F:\Text\ceshi";
private void Exist_Click(object sender, EventArgs e)
{
bool ifExist = Directory.Exists(path);
if (ifExist)
{
MessageBox.Show("已存在");
}
else
{
MessageBox.Show("不存在");
}
}
方法二:
private string path = @"F:\Text\ceshi";
private void Exist_Click(object sender, EventArgs e)
{
DirectoryInfo ExistDirectory = new DirectoryInfo(path);
bool ifExist = ExistDirectory.Exists;
if (ifExist)
{
MessageBox.Show("已存在");
}
else
{
MessageBox.Show("不存在");
}
}
(四)获取子文件夹
注:获取子文件只能是获取--该路径下的文件夹,其他非文件夹格式获取不到
方法一:
private void GetDirectory_Click(object sender, EventArgs e)
{
string[] Dire = Directory.GetDirectories(@"F:\Text");//返回的字符串是全路径加文件夹名称(如:"F:\Text\ceshi")
listBox_GetDiretory.Items.Clear();
listBox_GetDiretory.Items.AddRange(Dire);
}
方法二:
private void GetDirectory_Click(object sender, EventArgs e)
{
DirectoryInfo GetDirectory = new DirectoryInfo(@"F:\Text");//只返回文件夹的名字
DirectoryInfo[] Dire = GetDirectory.GetDirectories();
listBox_GetDiretory.Items.Clear();
listBox_GetDiretory.Items.AddRange(Dire);
}
(五)获取子文件
注:获取子文件只获取--除了文件夹以外其他的文件
方法一:
private void GetFile_Click(object sender, EventArgs e)
{
string[] file = Directory.GetFiles(@"F:\Text");//返回的字符串是全路径加文件名称(如:"F:\Text\ceshi")
listBox_GetFile.Items.AddRange(file);
}
方法二:
private void GetFile_Click(object sender, EventArgs e)
{
DirectoryInfo GetFile = new DirectoryInfo(@"F:\Text");//只返回文件名称
FileInfo[] file = GetFile.GetFiles();
listBox_GetFile.Items.AddRange(file);
}
(六)获取文件夹的相关属性
注:日期时间后面Utc代表格列尼日时间
方法一:
private string path = @"F:\Text\ceshi";
private void GetInfo_Click(object sender, EventArgs e)
{
DateTime CreateTime = Directory.GetCreationTime(path); //获取创建时间
DateTime AccessTime = Directory.GetLastAccessTime(path);//获取最后访问时间
DateTime WriteTime = Directory.GetLastWriteTime(path); //获取最后写入的时间
DirectoryInfo.Text = CreateTime.ToString() + "\n" + AccessTime.ToString() + "\n" + WriteTime.ToString();
}
方法二:
private string path = @"F:\Text\ceshi";
private void GetInfo_Click(object sender, EventArgs e)
{
DirectoryInfo DireInfo = new System.IO.DirectoryInfo(path);
Info.Text = DireInfo.CreationTime.ToString() + "\n" + DireInfo.LastAccessTime.ToString() + "\n" + DireInfo.LastWriteTime.ToString();
}
(七)修改文件夹的相关属性
方法一:
private string path = @"F:\Text\ceshi";
private void SetInfo_Click(object sender, EventArgs e)
{
DateTime T = DateTime.Now;
Directory.SetCreationTime(path, T);
Directory.SetLastAccessTime(path, T);
Directory.SetLastWriteTime(path, T);
}
方法二:
(.............)
(八)移动文件夹(或改名)
注:
Move()或MoveTo():1.路径不同名字相同(相当于--剪切),
2.路径不同名字不同(相当于--剪切+重命名),
3.路径相同名字不同(相当于--重命名).
且移动是同时将文件夹内的所有对象,移动.
方法一:
private string path = @"F:\Text\ceshi";
private void MoveDirectory_Click(object sender, EventArgs e)
{
Directory.Move(path, @"F:\Test");
}
方法二:
private string path = @"F:\Text\ceshi";
private void MoveDirectory_Click(object sender, EventArgs e)
{
DirectoryInfo MoveDirectory = new DirectoryInfo(path);
MoveDirectory.MoveTo(@"F:\ceshi2");
}