转http://skybirdzw.blog.163.com/blog/static/7257062620099751329403/

文件处理-Directory类 (C#)

Directory.CreateDirectory 方法:创建指定路径中的所有目录。

Directory.CreateDirectory (String)按 path 的指定创建所有目录和子目录。

Directory.CreateDirectory (String, DirectorySecurity)创建指定路径中的所有目录,并应用指定的 Windows 安全性。

//***************codeTest**************************************

using System;

using System.IO;

class Test

{

public static void Main()

{

// Specify the directory you want to manipulate.

string path = @"c:\MyDir\MyDoc";

try

{

// Determine whether the directory exists.

if (Directory.Exists(path))

{

Console.WriteLine("That path exists already.");

return;

}

// Try to create the directory.

DirectoryInfo di = Directory.CreateDirectory(path);

Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));

// Delete the directory.

//di.Delete();

Console.WriteLine("The directory was deleted successfully.");

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

finally { }

}

}

//***************endCode**************************************

Directory.Delete 方法:删除指定的目录。

Directory.Delete (String)从指定路径删除空目录。

Directory.Delete (String, Boolean)删除指定的目录并(如果指示)删除该目录中的任何子目录。

//***************codeTest**************************************

using System;

using System.IO;

class Test

{

public static void Main()

{

// Specify the directories you want to manipulate.

string path = @"c:\MyDir";

string subPath = @"c:\MyDir\temp";

try

{

// Determine whether the directory exists.

if (!Directory.Exists(path))

{

// Create the directory.

Directory.CreateDirectory(path);

}

if (!Directory.Exists(subPath))

{

// Create the directory.

Directory.CreateDirectory(subPath);

}

// This will succeed because subdirectories are being deleted.

Console.WriteLine("I am about to attempt to delete {0}", path);

Directory.Delete(path, true);

Console.WriteLine("The Delete operation was successful.");

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

finally { }

}

}

//***************endCode**************************************

Directory.Exists 方法:确定给定路径是否引用磁盘上的现有目录。

Directory.GetCreationTime 方法:获取目录的创建日期和时间。

//***************codeTest**************************************

using System;

using System.IO;

class Test

{

public static void Main()

{

try

{

// Get the creation time of a well-known directory.

DateTime dt = Directory.GetCreationTime(Environment.CurrentDirectory);

// Give feedback to the user.

if (DateTime.Now.Subtract(dt).TotalDays > 364)

{

Console.WriteLine("This directory is over a year old.");

}

else if (DateTime.Now.Subtract(dt).TotalDays > 30)

{

Console.WriteLine("This directory is over a month old.");

}

else if (DateTime.Now.Subtract(dt).TotalDays <= 1)

{

Console.WriteLine("This directory is less than a day old.");

}

else

{

Console.WriteLine("This directory was created on {0}", dt);

}

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

}

}

//***************endCode**************************************

Directory.GetCurrentDirectory 方法:获取应用程序的当前工作目录。

//***************codeTest**************************************

using System;

using System.IO;

class Test

{

public static void Main()

{

try

{

// Get the current directory.

string path = Directory.GetCurrentDirectory();

string target = @"c:\temp";

Console.WriteLine("The current directory is {0}", path);

if (!Directory.Exists(target))

{

Directory.CreateDirectory(target);

}

// Change the current directory.

Environment.CurrentDirectory = (target);

if (path.Equals(Directory.GetCurrentDirectory()))

{

Console.WriteLine("You are in the temp directory.");

}

else

{

Console.WriteLine("You are not in the temp directory.");

}

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

}

}

//***************endCode**************************************

Directory.GetDirectories 方法:获取指定目录中子目录的名称。

Directory.GetDirectories (String)获取指定目录中子目录的名称。

Directory.GetDirectories (String, String)从当前目录获取与指定搜索模式匹配的目录的数组。

Directory.GetDirectories (String, String, SearchOption)获取当前目录中与指定搜索模式匹配并使用某个值确定是否在子目录中搜索的目录的数组。

//***************codeTest**************************************

using System;

using System.IO;

class SubDir

{

public static void Main()

{

string path = @"F:\课件\通信原理";

try

{

string[] dirs = Directory.GetDirectories(path);

Console.WriteLine("F:\\课件\\通信原理 子目录数 = {0}", dirs.Length);

foreach (string dir in dirs)

Console.WriteLine(dir);

}

catch (Exception e)

{

Console.WriteLine("失败: {0}", e.ToString());

}

}

}

//***************endCode**************************************

//***************codeTest**************************************

using System;

using System.IO;

class Test

{

public static void Main()

{

try

{

// Only get subdirectories that begin with the letter "p."

string[] dirs = Directory.GetDirectories(@"c:\", "p*");

Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);

foreach (string dir in dirs)

{

Console.WriteLine(dir);

}

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

}

}

//***************endCode**************************************

Directory.GetFiles 方法:返回指定目录中的文件的名称。

Directory.GetFiles (String)返回指定目录中的文件的名称。

Directory.GetFiles (String, String)返回指定目录中与指定搜索模式匹配的文件的名称。

Directory.GetFiles (String, String, SearchOption)返回指定目录中文件的名称,该目录与指定搜索模式匹配并使用某个值确定是否在子目录中搜索。

//***************codeTest**************************************

using System;

using System.IO;

class Test

{

public static void Main()

{

try

{

// Only get files that begin with the letter "c."

string[] dirs = Directory.GetFiles(@"F:\memo","*.txt");

Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);

foreach (string dir in dirs)

{

Console.WriteLine(dir);

}

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

}

}

//***************endCode**************************************

Directory.GetFileSystemEntries 方法:返回指定目录中所有文件和子目录的名称。

Directory.GetFileSystemEntries (String)返回指定目录中所有文件和子目录的名称。

Directory.GetFileSystemEntries (String, String)返回与指定搜索条件匹配的文件系统项的数组。

Directory.Move 方法:将文件或目录及其内容移到新位置。

//***************codeTest**************************************

using System;

using System.IO;

class Test

{

public static void Main()

{

try

{

string path = @"F:\memo";

string path2 = @"F:\VC++串口通讯\memo";

Directory.Move(path, path2);

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

}

}

//***************endCode**************************************

举 例来讲,如果您尝试将 c:\mydir 移到 c:\public,并且 c:\public 已存在,则此方法引发 IOException。您必须将“c:\\public\\mydir”指定为 destDirName 参数(假设“c:\\public”下不存在“mydir”),或者指定一个新的目录名,例如“c:\\newdir”。

//***************codeTest**************************************

using System;

namespace GetFileSystemEntries

{

class Class1

{

static void Main(string[] args)

{

Class1 snippets = new Class1();

string path = System.IO.Directory.GetCurrentDirectory();

string filter = "*.exe";

snippets.PrintFileSystemEntries(path);

snippets.PrintFileSystemEntries(path, filter);

snippets.GetLogicalDrives();

snippets.GetParent(path);

snippets.Move("C:\\proof", "C:\\Temp");

}

void PrintFileSystemEntries(string path)

{

try

{

// Obtain the file system entries in the directory path.

string[] directoryEntries =

System.IO.Directory.GetFileSystemEntries(path);

foreach (string str in directoryEntries)

{

System.Console.WriteLine(str);

}

}

catch (ArgumentNullException)

{

System.Console.WriteLine("Path is a null reference.");

}

catch (System.Security.SecurityException)

{

System.Console.WriteLine("The caller does not have the " +

"required permission.");

}

catch (ArgumentException)

{

System.Console.WriteLine("Path is an empty string, " +

"contains only white spaces, " +

"or contains invalid characters.");

}

catch (System.IO.DirectoryNotFoundException)

{

System.Console.WriteLine("The path encapsulated in the " +

"Directory object does not exist.");

}

}

void PrintFileSystemEntries(string path, string pattern)

{

try

{

// Obtain the file system entries in the directory

// path that match the pattern.

string[] directoryEntries =

System.IO.Directory.GetFileSystemEntries(path, pattern);

foreach (string str in directoryEntries)

{

System.Console.WriteLine(str);

}

}

catch (ArgumentNullException)

{

System.Console.WriteLine("Path is a null reference.");

}

catch (System.Security.SecurityException)

{

System.Console.WriteLine("The caller does not have the " +

"required permission.");

}

catch (ArgumentException)

{

System.Console.WriteLine("Path is an empty string, " +

"contains only white spaces, " +

"or contains invalid characters.");

}

catch (System.IO.DirectoryNotFoundException)

{

System.Console.WriteLine("The path encapsulated in the " +

"Directory object does not exist.");

}

}

// Print out all logical drives on the system.

void GetLogicalDrives()

{

try

{

string[] drives = System.IO.Directory.GetLogicalDrives();

foreach (string str in drives)

{

System.Console.WriteLine(str);

}

}

catch (System.IO.IOException)

{

System.Console.WriteLine("An I/O error occurs.");

}

catch (System.Security.SecurityException)

{

System.Console.WriteLine("The caller does not have the " +

"required permission.");

}

}

void GetParent(string path)

{

try

{

System.IO.DirectoryInfo directoryInfo =

System.IO.Directory.GetParent(path);

System.Console.WriteLine(directoryInfo.FullName);

}

catch (ArgumentNullException)

{

System.Console.WriteLine("Path is a null reference.");

}

catch (ArgumentException)

{

System.Console.WriteLine("Path is an empty string, " +

"contains only white spaces, or " +

"contains invalid characters.");

}

}

void Move(string sourcePath, string destinationPath)

{

try

{

System.IO.Directory.Move(sourcePath, destinationPath);

System.Console.WriteLine("The directory move is complete.");

}

catch (ArgumentNullException)

{

System.Console.WriteLine("Path is a null reference.");

}

catch (System.Security.SecurityException)

{

System.Console.WriteLine("The caller does not have the " +

"required permission.");

}

catch (ArgumentException)

{

System.Console.WriteLine("Path is an empty string, " +

"contains only white spaces, " +

"or contains invalid characters.");

}

catch (System.IO.IOException)

{

System.Console.WriteLine("An attempt was made to move a " +

"directory to a different " +

"volume, or destDirName " +

"already exists.");

}

}

}

}

//***************endCode**************************************

Notice: Codes were mainly from MSDN

文件处理-Directory类 (C#)的更多相关文章

  1. Directory类和DirectoryInfo类

    1.Directory类 Directory类公开了用于创建.移动.枚举.删除目录和子目录的静态方法 2.DirectoryInfo类 DirectoryInfo和Directory类的区别可以参看F ...

  2. .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化

    1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...

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

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

  4. C#文件操作常用相关类(Directory类、File类、Path类)

    1.文件操作常用相关类 1)File //操作文件,静态类,对文件整体操作.拷贝.删除.剪切等 2)Directory //操作目录(文件夹),静态类 3)DirectoryInfo //文件夹的一个 ...

  5. I/O Directory类

    Directory类 Directory类位于System.IO 命名空间.Directory类提供了在目录和子目录中进行创建移动和列举操作的静态方法.此外,你还可以访问和操作各种各样的目录属性. 1 ...

  6. C#读取XML文件的基类实现

    刚到新单位,学习他们的源代码,代码里读写系统配置文件的XML代码比较老套,直接写在一个系统配置类里,没有进行类的拆分,造成类很庞大,同时,操作XML的读写操作都是使用SetAttribute和node ...

  7. System.IO.Directory类

    1.参考的博客:System.IO.Directory类和System.DirectoryInfo类(http://blog.sina.com.cn/s/blog_614f473101017du4.h ...

  8. ASP.NET 文件上传类 简单好用

    调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...

  9. C#文件读写常用类介绍

    首先要熟悉.NET中处理文件和文件夹的操作.File类和Directory类是其中最主要的两个类.了解它们将对后面功能的实现提供很大的便利.      本节先对和文件系统相关的两个.NET类进行简要介 ...

随机推荐

  1. S5PV210的IRAM应用

    准备分析   IRAM的大小96k,其实前两个程序都在这里运行的,程序都小于16K.要实现的是从把IRAM从的前16k从IRAM的起始地址0xD0020000拷贝到0xD0024000 处,调用mai ...

  2. 【HDOJ】2430 Beans

    这题目用线段树超时了,其实也差不多应该超时.10^6大数据量.看了一下网上的解法是单调队列.大概了解了一下,是个挺有意思的数据结构.首先,需要求满足0<=(S[r]-S[l])%p<=k时 ...

  3. CSS属性一览

    CSS 属性 CSS 属性组: 动画 背景 边框和轮廓 盒(框) 颜色 内容分页媒体 定位 可伸缩框 字体 生成内容 网格 超链接 行框 列表 外边距 Marquee 多列 内边距 分页媒体 定位 打 ...

  4. Eclipse Mars: How to Stop Updating Error Reporting Database

    Eclipse Mars: How to Stop Updating Error Reporting Database I was using Eclise Mars version IDE.. Ev ...

  5. 根据价格范围筛选汽车(路由以及JS与Jquery)

    通过输入价格范围,来筛选汽车,主要方法是通过点击“查询”按钮,触发chaxun()方法,利用Jquery和JS获取输入的值,然后为相应的div加载相应的动作,通过更改路由的路径,以此来实现筛选车辆,然 ...

  6. cnb

    <style type="text/css"> .fieldset_s{border: 1px #dedede solid;padding: 19px; color: ...

  7. 【转】NI语法 JNI参考 JNI函数大全

    原文网址:http://blog.sina.com.cn/s/blog_5de73d0b0101chk1.html 一.对照表 Java类型    本地类型         描述boolean     ...

  8. JavaScript里的类和继承

    JavaScript与大部分客户端语言有几点明显的不同: JS是 动态解释性语言,没有编译过程,它在程序运行过程中被逐行解释执行JS是 弱类型语言,它的变量没有严格类型限制JS是面向对象语言,但 没有 ...

  9. 彻底解决:请求被中止: 未能创建 SSL/TLS 安全通道

    最近有个项目要调用客户用java写的带https的webservice,对方提供了证书文件 test.pfx,我这里调用方式如下: //webservice代理类 SvcService svc = n ...

  10. OAuth 的权限问题与信息隐忧

    核心提示:以 QQ 登陆和微博登陆为代表的“一键登陆”背后不仅仅是登陆这么简单,它还默认获取了你的其他隐私资料和账号的部分使用权限,我们在享受便利的同时一定不要忘记保护好我们的个人信息安全. 去年3Q ...