一、文件夹操作

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. SQL——存储过程

    1. 为什么使用存储过程 应用程序通过T-SQL语句到服务器的过程是不安全的. 1) 数据不安全 2)每次提交SQL代码都要经过语法编译后在执行,影响应用程序的运行性能 3) 网络流量大 2. 什么是 ...

  2. Java 中的System.exit

    在java 中退出程序,经常会使用System.exit(1) 或 System.exit(0). 查看System.exit()方法的源码,如下 /** * Terminates the curre ...

  3. html 复习

    通过几次修改网页的经历,发现相关基础知识之薄弱,不得不再次花时间复习一遍.希望这是最后一次. 一 通用声明 HTML5 <!DOCTYPE html> HTML 4.01 <!DOC ...

  4. Valid Anagram

    class Solution { public: bool isAnagram(string s, string t) { if(t=="") return s=="&q ...

  5. Binary Tree Level Order Traversal II

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  6. Hosting custom WPF calendar control in AX 2012

    原作者: https://community.dynamics.com/ax/b/axilicious/archive/2013/05/20/hosting-custom-wpf-calendar-c ...

  7. Python使用报错记录

    问题1:pip 报错 C:\Users\Administrator>pip3 install pyreadline Fatal error in launcher: Unable to crea ...

  8. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

  9. Asp.net Vnext Routing

    概述 本文已经同步到<Asp.net Vnext 系列教程 >中] ASP.NET 路由系统是主要负责两个操作: 它将传入的 HTTP 请求映射到路由处理程序给出的路由的集合. 路由系统的 ...

  10. 单利 复利计算器程序1.0 2.0 3.0 [ 合 ] 之 WEB

    对单复利计算器程序进行改进 更新为网页版的. 界面不太美观 请谅解 由于时间问题暂未完善好! 计算部分的主要源代码: