文章转自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. npm如何删除node_modules文件夹

    npm install rimraf -g 先安装删除工具,然后使用删除命令 rimraf node_modules

  2. C++ 右值引用与移动操作

    右值引用和移动操作是C++11提出的新概念,通过这些操作,可以降低拷贝操作带来的消耗.先来简单介绍一下左值和右值. 左值一般指的是一个对象,或者说是一个持久的值,例如赋值的返回值.下标操作.解引用以及 ...

  3. pysam操作sam文件

    pysam模块 因为要分析sam文件中序列的情况,因此要对reads进行细分,所以之前想用数据库将sam文件信息存储,然后用sql语句进行分类.后来发现很麻烦,pysam就是一个高效读取存储在SAM ...

  4. 《Java多线程编程核心技术》读后感(十五)

    线程的状态 线程对象在不同的运行时期有不同的状态,状态信息就存在与State枚举类中. 验证New,Runnable,Terminated new:线程实例化后还从未执行start()方法时的状态 r ...

  5. skb详细解析【转】

    skb详细解析[转]    摘自:http://blog.chinaunix.net/uid-30035229-id-4883992.html     在自己的模块发送函数中,需要对skb进行重新构造 ...

  6. Hibernate的auto-import属性详解

    auto-import是什么意思呢? 我们经常会写这样一个HQL语句: from User u where u.name='罗灿锋'; 绝大多数时候,这样写是不会发生问题的. hibernate在处理 ...

  7. php封装pdo操作数据的工具类

    <?php header("Content-Type:text/html;charset=utf-8"); class PdoMysql{ public static $co ...

  8. UML 学习地址

    UML 学习地址 网址:http://www.uml.org.cn/oobject/201609092.asp http://www.uml.org.cn/oobject/201009081.asp ...

  9. SqlServer2012——多表连接查询

    1.基本连接 select A.姓名,A.性别,B.班级名,B.家庭住址 From 学生信息 A,班级信息 B where A.所属班级=B.班级编号 --把A表与B表连接起来 2.内连接 --内连接 ...

  10. Elasticsearch Query DSL查询入门

    本篇为学习DSL时做的笔记,适合ES新手,大佬请略过~ Query DSL又叫查询表达式,是一种非常灵活又富有表现力的查询语言,采用JSON接口的方式实现丰富的查询,并使你的查询语句更灵活.更精确.更 ...