C#中如何创建文件夹
来源:互联网 发布:家用灭蚊 知乎 编辑:程序博客网 时间:2024/06/05 15:38
C#中如何创建文件夹
C#中对文件夹操作需要用到Directory Class。其中提供了创建、删除、移动、枚举等静态方法。该类不能被继承。
以下代码实现了创建文件夹。
1
2
3
4
if
(!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);
}
以下是MSDN上Directory Class的Sample code。
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
以下代码首先检查指定的文件夹是否存在,若存在则删除之,否则创建之。接下来移动文件夹,在其中创建文件并统计文件夹中文件数目。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
{}
}
}
以下代码演示了如何计算文件夹大小。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 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 = 0;
// 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 != 1)
{
Console.WriteLine(
"You must provide a directory argument at the command line."
);
}
else
{
DirectoryInfo d =
new
DirectoryInfo(args[0]);
long
dsize = DirSize(d);
Console.WriteLine(
"The size of {0} and its subdirectories is {1} bytes."
, d, dsize);
}
}
}
转自:http://www.cnblogs.com/pegasus923/archive/2011/01/26/1944838.html
0 0
- C#中如何创建文件夹
- C#中如何创建文件夹
- C# 如何创建一个文件夹
- C#中动态创建文件夹及文件
- genexus 中如何能创建文件夹
- ios如何在xcode中创建文件夹
- Python中如何创建文件夹和嵌套文件夹的方法
- c#中如何打开一个文件夹
- Fedora中如何创建文件夹和创建文件
- C#创建文件夹
- c#创建文件夹类
- C#创建文件夹,文件
- c#递归创建文件夹
- C#创建文件夹
- File中如何能创建多个新文件夹,并在新文件夹中创建新文件呢?
- 如何创建共享文件夹?
- 如何创建无名文件夹?
- 如何创建删不掉文件夹
- 瀑布流思路及源码
- 【转】如何实现点击textbox显示monthCalendar 选择monthCalendar把值传给textbox
- js 正则表达式用法
- 线程间操作无效: 从不是创建控件“”的线程访问它的解决方法~
- eclipse老是building workspace及自动更新问题,eclipse加速
- C#中如何创建文件夹
- android为App签名(为apk签名)
- SVN 代码提交操作步骤
- 【设计模式篇】Android设计模式之-组合模式 自我见解
- android:为TextView添加样式——下划线,颜色,设置链接样式及前背景色
- iOS QQ登录 傻瓜式集成
- 某些手机(Andreno gpu)上gpu渲染异常的问题。(文章原题:CCRenderTexture 从后台进入前台变黑的处理(android))
- 瞎写。
- HIve实战分析Hadoop的日志