C# 简单创建和删除文件夹
文章转自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# 简单创建和删除文件夹的更多相关文章
- iOS创建、删除文件夹、获取沙盒路径
1.获取沙盒路径 // 获取沙盒路径 NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent: ...
- ios 下创建,删除文件夹的方法
NSString *imageDir = [NSString stringWithFormat:@"%@/Caches/%@", NSHomeDirectory(), dirNam ...
- C# 删除文件夹
三种方法 1.这种方法简单,能删除文件夹内的所有文件(文件及子目录) DirectoryInfo di = new DirectoryInfo(string Path); di.Del ...
- Linux 删除文件夹和创建文件的命令
删除文件夹实例:rm -rf /var/log/httpd/access将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 删除文件使用实例: rm -f /var/log ...
- 学习Linux二(创建、删除文件和文件夹命令)
转自:http://www.cnblogs.com/zf2011/archive/2011/05/17/2049155.html 今天学习了几个命令,是创建.删除文件和文件夹的,在linux里,文件 ...
- centos彻底删除文件夹创建文件
centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建文件夹 mkdir 文件名 新建一个名为test的文件夹在home下 view source1 mkdir ...
- Python基础之创建文件夹与删除文件夹。
参考链接:https://blog.csdn.net/weixin_43826242/article/details/87101436 创建目录结构 # 创建文件目录结构 def create_fol ...
- 【File】递归删除文件夹中子级文件/夹,并删除文件夹
今天有这样一个需求,需要删除某一个文件夹,但是文件夹中还有子级的文件 或者还可能会有文件夹在里面,所以就需要使用一个简单的递归才能将文件夹删除成功,包括文件夹中的子级文件/夹.!!! 其实很简单,就一 ...
- Linux 删除文件夹和文件命令
inux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就是 ...
随机推荐
- java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap 解决方法
转自:https://blog.csdn.net/q664243624/article/details/69221324
- MSSQl分布式查询(转)
MSSQlServer所谓的分布式查询(Distributed Query)是能够访问存放在同一部计算机或不同计算机上的SQL Server或不同种类的数据源, 从概念上来说分布式查询与普通查询区别 ...
- 32bit / 64bit co-exist Linux, ld-linux.so, linux-gate.so.1 etc
before this, confirm that you don't have 32bit libs notably 32bit libc, e.g. you have /lib64/ld-linu ...
- 24.集成ASP.NETCore Identity
正常的情况下view页面的错误的显示应该是这么去判断的 这里我们就不加判断为了,直接用这个div 显示就可以了.当有错误会自动显示在div内 asp.net core Identity加入进来 这里用 ...
- 3. 关于sql注入的综合题
关于sql注入的综合题 ----------南京邮电大学ctf : http://cms.nuptzj.cn/ 页面上也给了好多信息: 根据这个sm. ...
- Depth Buffer
Up until now there is only one type of output buffer you've made use of, the color buffer. This chap ...
- 拓展kmp模板
算法描述:设字符串T,长度为n,字符串S,长度为m.在线性时间内求出T的每一个后缀所对应S的最长前缀. 假设T=“AAAAB”,S="AAAA". //拓展KMP ; //字符串长 ...
- [小工具] C#多线程|匿名委托传参数|测试网站压力--升级版
上次文章链接:http://www.sufeinet.com/thread-11-1-1.html写这些并不是不会用测试工具,也并不是无视测试工具,而是做为一个程序员希望用自己写的东西来完成一些功能, ...
- sql server 2012 profiler打开2016的profiler
软件环境:1.本机是sql server 2012,远程服务器是sql server 20162.本机是Windows 7,安装不了sql server 2016 问题场景1:本机sql server ...
- LeetCode.908-最小差值 1(Smallest Range I)
这是悦乐书的第348次更新,第372篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第213题(顺位题号是908).给定一个整数数组A,对于每个整数A[i],我们可以选择任 ...