A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 陈君 金牌黑马   /  2014-8-15 17:36  /  1403 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

转自:http://www.jb51.net/article/37621.htm
本篇文章是对C#操作目录与文件的方法步骤进行了详细的分析介绍,需要的朋友参考下
• 创建目录和文件
1、通过Path类的Combine方法可以合并路径。

  1. string activeDir = @"C:\myDir";
  2. string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
复制代码

2、目录的创建。
创建目录时如果目录已存在,则不会重新创建目录,且不会报错。创建目录时会自动创建路径中各级不存在的目录。
(1)通过Directory类的CreateDirectory方法创建。

  1. string activeDir = @"C:\myDir";
  2. string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
  3. System.IO.Directory.CreateDirectory(newPath);
复制代码

(2)通过DirectoryInfo的对象创建。

  1. System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree");
  2. di.Create();
复制代码

3、文件的创建。
通过Create方法创建文件,会覆盖同名的现有文件。创建文件时,该文件所在路径的目录必须存在,否则报错。
(1)通过File类的Create方法创建。

  1. string activeDir = @"C:\myDir";
  2. string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
  3. System.IO.Directory.CreateDirectory(newPath);
  4. //创建一个空白文件
  5. string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
  6. + ".txt";
  7. string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
  8. System.IO.File.Create(filePathOne);
复制代码

(2)通过FileInfo对象创建。

  1. //通过Combine合并目录
  2. //然后创建目录
  3. string activeDir = @"C:\myDir";
  4. string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
  5. System.IO.Directory.CreateDirectory(newPath);
  6. //创建一个空白文件
  7. string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
  8. + ".txt";
  9. string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
  10. System.IO.FileInfo fi = new System.IO.FileInfo(filePathOne);
  11. fi.Create();
复制代码

• 复制目录文件

  1. //复制单个文件到指定目录
  2. string fileName = "test.txt";
  3. string sourcePath = @"C:\testDir\subTestDir";
  4. string targetPath = @"C:\testDir\subTestDirTwo";
  5. string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
  6. string destFile = System.IO.Path.Combine(targetPath, fileName);
  7. if (!System.IO.Directory.Exists(targetPath))
  8. System.IO.Directory.CreateDirectory(targetPath);
  9. //如果已存在,参数为false时将报错,参数为true重写该文件
  10. //当copy方法为两个参数时,默认重写为false。
  11. System.IO.File.Copy(sourceFile, destFile, true);
  12. //以下为复制一个目录下所有文件到指定目录
  13. //如果复制有子目录的目录的所有文件,可以用递归或堆栈算法实现
  14. if (System.IO.Directory.Exists(sourcePath))
  15. {
  16. string[] files = System.IO.Directory.GetFiles(sourcePath);
  17. foreach (string s in files)
  18. {
  19. //仅返回路径字符串的文件名及后缀
  20. fileName = System.IO.Path.GetFileName(s);
  21. destFile = System.IO.Path.Combine(targetPath, fileName);
  22. System.IO.File.Copy(s, destFile, true);
  23. }
  24. }
  25. }
复制代码

• 移动目录和文件

  1. /*移动文件*/
  2. string sourceFile = @"C:\testDir\subTestDir\test.txt";
  3. string destFile = @"C:\testDir\subTestDirTwo\test.txt";
  4. //当目标文件存在时,抛出异常
  5. System.IO.File.Move(sourceFile, destFile);
  6. /*移动目录*/
  7. //移动目录将移动改目录的子目录和文件
  8. System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");
复制代码

• 创建目录和文件
1、通过Path类的Combine方法可以合并路径。
[url=]复制代码[/url] 代码如下:

string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");


2、目录的创建。
创建目录时如果目录已存在,则不会重新创建目录,且不会报错。创建目录时会自动创建路径中各级不存在的目录。
(1)通过Directory类的CreateDirectory方法创建。
[url=]复制代码[/url] 代码如下:

string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);


(2)通过DirectoryInfo的对象创建。
[url=]复制代码[/url] 代码如下:

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree");
di.Create();


3、文件的创建。
通过Create方法创建文件,会覆盖同名的现有文件。创建文件时,该文件所在路径的目录必须存在,否则报错。
(1)通过File类的Create方法创建。
[url=]复制代码[/url] 代码如下:

string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
//创建一个空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt";
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
System.IO.File.Create(filePathOne);


(2)通过FileInfo对象创建。
[url=]复制代码[/url] 代码如下:

//通过Combine合并目录
//然后创建目录
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);
//创建一个空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt";
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
System.IO.FileInfo fi = new System.IO.FileInfo(filePathOne);
fi.Create();


• 复制目录文件
[url=]复制代码[/url] 代码如下:

//复制单个文件到指定目录
string fileName = "test.txt";
string sourcePath = @"C:\testDir\subTestDir";
string targetPath = @"C:\testDir\subTestDirTwo";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
System.IO.Directory.CreateDirectory(targetPath);
//如果已存在,参数为false时将报错,参数为true重写该文件
//当copy方法为两个参数时,默认重写为false。
System.IO.File.Copy(sourceFile, destFile, true);
//以下为复制一个目录下所有文件到指定目录
//如果复制有子目录的目录的所有文件,可以用递归或堆栈算法实现
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string s in files)
{
//仅返回路径字符串的文件名及后缀
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
}


• 移动目录和文件
[url=]复制代码[/url] 代码如下:

/*移动文件*/
string sourceFile = @"C:\testDir\subTestDir\test.txt";
string destFile = @"C:\testDir\subTestDirTwo\test.txt";
//当目标文件存在时,抛出异常
System.IO.File.Move(sourceFile, destFile);
/*移动目录*/
//移动目录将移动改目录的子目录和文件
System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");


• 删除目录和文件
1、删除目录
删除目录,如果该目录不存在,会抛出异常。可以通过File类的Delete方法删除目录,也可以通过FileInfo对象方法删除目录。
(1)通过 File类的Delete方法删除目录

  1. //删除可写空目录
  2. //如果不为空抛出目录不为空异常
  3. try
  4. {
  5. System.IO.Directory.Delete(@"C:\testDir\subTestDir");
  6. }
  7. catch (System.IO.IOException e)
  8. {
  9. Console.WriteLine(e.Message);
  10. }
  11. //第二参数为false时,只能删除空目录,否则抛出不为空异常
  12. //第二参数为true时,删除目录,包括子目录和文件
  13. try
  14. {
  15. System.IO.Directory.Delete(@"C:\testDir\subTestDir", true);
  16. }
  17. catch(System.IO.IOException e)
  18. {
  19. Console.WriteLine(e.Message);
  20. }
复制代码

(2)通过FileInfo对象方法删除目录

  1. System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo");
  2. try
  3. {
  4. //无参数删除空目录
  5. //当参数为false,可删除空目录;为true,删除目录,包括子目录和文件
  6. di.Delete(true);
  7. }
  8. catch (System.IO.IOException e)
  9. {
  10. Console.WriteLine(e.Message);
  11. }
复制代码

2、删除文件
删除文件时如果指定文件的目录存在,而文件不存在,则不会抛出异常,如果指定文件的目录不存在,则会抛出异常。
(1)通过File类Delete方法删除文件

  1. try
  2. {
  3. System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt");
  4. }
  5. catch(System.IO.IOException e)
  6. {
  7. Console.WriteLine(e.Message);
  8. }
复制代码

(2)通过FileInfo对象Delete方法删除文件

  1. System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\testDir\subTestDir\test1.txt");
  2. try
  3. {
  4. fi.Delete();
  5. }
  6. catch(System.IO.IOException e)
  7. {
  8. Console.WriteLine(e.Message);
  9. }
复制代码








0 个回复

您需要登录后才可以回帖 登录 | 加入黑马