一、文件夹操作

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");
}

.Net文件*夹*操作的更多相关文章

  1. [No000083]文件与文件夹操作

    #region Folder option 文件夹操作 /// <summary> /// 指定目录是否存在 /// </summary> /// <param name ...

  2. PHP 文件夹操作「复制、删除、查看大小」递归实现

    PHP虽然提供了 filesize.copy.unlink 等文件操作的函数,但是没有提供 dirsize.copydir.rmdirs 等文件夹操作的函数(rmdir也只能删除空目录).所以只能手动 ...

  3. iOS开发——Swift篇&文件,文件夹操作

    文件,文件夹操作   ios开发经常会遇到读文件,写文件等,对文件和文件夹的操作,这时就可以使用NSFileManager,NSFileHandle等类来实现. 下面总结了各种常用的操作:   1,遍 ...

  4. Python的文件与文件夹操作

    Python的文件与文件夹操作 Python OS模块 1.重命名:os.rename(old, new) 2.删除:os.remove(file) 3.列出目录下的文件 :os.listdir(pa ...

  5. linux —— 学习笔记(文件、文件夹操作)

    目录:1.常用的文件文件夹操作 2.文件属性的设置 1.常用的文件文件夹操作 mkdir  创建文件夹 -p 如果指定 a/b/c 时 a .b 不存在,一起创建出来 cp       复制文件或文件 ...

  6. c# 封装的文件夹操作类之复制文件夹

    c#  封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs /// <summary> /// 文件夹操作类 /// ...

  7. Python_文件与文件夹操作

    ''' os模块除了提供使用操作系统功能和访问文件系统的简便方法之外,还提供了大量文件与文件夹操作的方法. os.path模块提供了大量用于路径判断.切分.连接以及文件夹遍历的方法. shutil模块 ...

  8. 文件及文件夹操作- File类、Directory 类、FileInfo 类、DirectoryInfo 类

    文件及文件夹操作: C/S:WinForm可以操作客户端文件 Client ServerB/S:Brower Server 命名空间:using system .IO; 1. File类: 创建:Fi ...

  9. python文件、文件夹操作OS模块

    转自:python文件.文件夹操作OS模块   '''一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: ...

随机推荐

  1. paper 3:matlab中save,load使用方法小结

    功能描述]存储文件[软件界面]MATLAB->File->Save Workspace As将变量存入硬盘中指定路径.[函数用法] save:该函数将所有workspace中变量用二进制格 ...

  2. MVC权限管理系统dwpro项目分配按钮没有显示的问题

    问题如下: 修改如下: 或者(原因为这个两个地方名要一致,大小写也要注意): 效果图:

  3. zw版【转发·台湾nvp系列Delphi例程】HALCON RegionToBin2

    zw版[转发·台湾nvp系列Delphi例程]HALCON RegionToBin2 unit Unit1;interfaceuses Windows, Messages, SysUtils, Var ...

  4. 一个容易被忽略的ReportingService超时问题

    我们在使用Sql Server Reporting Service开发报表的时候,经常会遇到报表超时的问题,报表超时的原因有很多,也有很多地方可以设置报表的超时时间,比如在报表中的数据源(dataso ...

  5. UISegmentedControl(转)

    初始化UISegmentedControl NSArray *arr = [[NSArray alloc]initWithObjects:@"轻拍",@"长按" ...

  6. jsp struts标签迭代各种数据

    首先创建一个User对象 User user=new User(); user.setUserName("张三"); user.setAge(30); User user1=new ...

  7. android 学习随笔二十八(应用小知识点小结 )

    去掉标题栏的方法 第一种:也一般入门的时候经常使用的一种方法requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏注意这句一定要写在setConte ...

  8. datatables中columns.render的使用

    可直接在columns申明中对应列下方使用render改变该列样式 也可单独在columnsDefs中用targets指定目标. "render":function(data,ty ...

  9. drupal 自定义表单调用autocomplete主标签实现方法

    代码如下: <php function module_name_form() { $form = array(); $form['city'] = array( '#title' => t ...

  10. PHP获取不了React Native Fecth参数的解决办法是怎么样呢?

    fetch('https://mywebsite.com/endpoint/', { method: 'POST',headers: {'Accept': 'application/json','Co ...