文章转自http://www.cnblogs.com/pegasus923/archive/2011/01/26/1944838.html

C#中对文件夹操作需要用到Directory Class。其中提供了创建、删除、移动、枚举等静态方法。该类不能被继承。

以下代码实现了创建文件夹。

if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);
}

以下是MSDN上Directory Class的Sample code。

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

以下代码首先检查指定的文件夹是否存在,若存在则删除之,否则创建之。接下来移动文件夹,在其中创建文件并统计文件夹中文件数目。

using System;
using System.IO; class Test
{
public static void Main()
{
// Specify the directories you want to manipulate.
string path = @"c:\MyDir";
string target = @"c:\TestDir"; try
{
// Determine whether the directory exists.
if (!Directory.Exists(path))
{
// Create the directory it does not exist.
Directory.CreateDirectory(path);
} if (Directory.Exists(target))
{
// Delete the target to ensure it is not there.
Directory.Delete(target, true);
} // Move the directory.
Directory.Move(path, target); // Create a file in the directory.
File.CreateText(target + @"\myfile.txt"); // Count the files in the target directory.
Console.WriteLine("The number of files in {0} is {1}",
target, Directory.GetFiles(target).Length);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
}
}

以下代码演示了如何计算文件夹大小。

// The following example calculates the size of a directory
// and its subdirectories, if any, and displays the total size
// in bytes. using System;
using System.IO; public class ShowDirSize
{
public static long DirSize(DirectoryInfo d)
{
long Size = ;
// Add file sizes.
FileInfo[] fis = d.GetFiles();
foreach (FileInfo fi in fis)
{
Size += fi.Length;
}
// Add subdirectory sizes.
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)
{
Size += DirSize(di);
}
return(Size);
}
public static void Main(string[] args)
{
if (args.Length != )
{
Console.WriteLine("You must provide a directory argument at the command line.");
}
else
{
DirectoryInfo d = new DirectoryInfo(args[]);
long dsize = DirSize(d);
Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, dsize);
}
}
}

C# 简单创建和删除文件夹的更多相关文章

  1. iOS创建、删除文件夹、获取沙盒路径

    1.获取沙盒路径 // 获取沙盒路径 NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent: ...

  2. ios 下创建,删除文件夹的方法

    NSString *imageDir = [NSString stringWithFormat:@"%@/Caches/%@", NSHomeDirectory(), dirNam ...

  3. C# 删除文件夹

    三种方法 1.这种方法简单,能删除文件夹内的所有文件(文件及子目录) DirectoryInfo di = new DirectoryInfo(string Path);         di.Del ...

  4. Linux 删除文件夹和创建文件的命令

    删除文件夹实例:rm -rf /var/log/httpd/access将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 删除文件使用实例: rm -f /var/log ...

  5. 学习Linux二(创建、删除文件和文件夹命令)

     转自:http://www.cnblogs.com/zf2011/archive/2011/05/17/2049155.html 今天学习了几个命令,是创建.删除文件和文件夹的,在linux里,文件 ...

  6. centos彻底删除文件夹创建文件

    centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建文件夹 mkdir 文件名 新建一个名为test的文件夹在home下 view source1 mkdir ...

  7. Python基础之创建文件夹与删除文件夹。

    参考链接:https://blog.csdn.net/weixin_43826242/article/details/87101436 创建目录结构 # 创建文件目录结构 def create_fol ...

  8. 【File】递归删除文件夹中子级文件/夹,并删除文件夹

    今天有这样一个需求,需要删除某一个文件夹,但是文件夹中还有子级的文件 或者还可能会有文件夹在里面,所以就需要使用一个简单的递归才能将文件夹删除成功,包括文件夹中的子级文件/夹.!!! 其实很简单,就一 ...

  9. Linux 删除文件夹和文件命令

    inux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就是 ...

随机推荐

  1. java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter

    转自:https://blog.csdn.net/rchm8519/article/details/23788053 1. ERROR - Context initialization failedo ...

  2. boost编译配置及简单使用

    boost编译配置及简单使用 1.下载 http://www.boost.org/ 2.编译: A.解压 boost_1_55_0.zip 到boost路径 B.运行 bootstrap.bat. 会 ...

  3. 条款29:为“异常安全”而努力是值得的

    当异常被抛出时,带有异常安全性的函数: 1.不泄露任何资源 2.不允许数据败坏   异常安全函数提供以下三个保证之一: 1.基本承诺:如果异常被抛出,程序内的任何事物仍然保持在有效的状态下.没有任何对 ...

  4. framework资源文件读取

    1.在framework里面读framwork自己的资源文件 这是framework内部的资源,跟其他都没有关系.但是framework不能单独存在,必须要放在某个“主程序”中才能起作用.bundle ...

  5. CLR via C# 第五章学习记录(更新中)

    1.设置全局溢出检查,项目属性->生成->高级->检测运算上溢/下溢 2.局部使用溢出检测 Byte b = ; b = ));// 不检测溢出 checked// 检测溢出代码段 ...

  6. CSS 绝对定位与相对定位的区别

    设置为绝对定位的元素框从文档流完全删除, 并相对于其包含块定位,包含块可能是文档中的另一个元素或者是初始包含块. 元素原先在正常文档流中所占的空间会关闭,就好像该元素原来不存在一样. 元素定位后生成一 ...

  7. 洛谷 P1875 佳佳的魔法药水

    P1875 佳佳的魔法药水 题目描述 发完了 k 张照片,佳佳却得到了一个坏消息:他的 MM 得病了!佳佳和大家一样焦急 万分!治好 MM 的病只有一种办法,那就是传说中的 0 号药水 --怎么样才能 ...

  8. vue中axios开启cookies

  9. M-HJ浇花

    题目描述: 链接:https://ac.nowcoder.com/acm/contest/322/M来源:牛客网 HJ养了很多花(99999999999999999999999999999999999 ...

  10. Selenium 开源书(一): Selenium历史

    Selenium历史 Selenium最初由Jason Huggins于2004年开发,作为ThoughtWorks的内部工具.Huggins后来加入了ThoughtWorks的其他程序员和测试人员, ...