zoukankan
html css js c++ java
C#文件和文件夹操作
//
1.文件夹创建、移动、删除---------
//
创建文件夹
Directory.CreateDirectory(Server.MapPath(
"
a
"
));
Directory.CreateDirectory(Server.MapPath(
"
b
"
));
Directory.CreateDirectory(Server.MapPath(
"
c
"
));
//
移动b到a
Directory.Move(Server.MapPath(
"
b
"
), Server.MapPath(
"
a\\b
"
));
//
删除c
Directory.Delete(Server.MapPath(
"
c
"
));
//
2.文件创建、复制、移动、删除---------
//
创建文件
//
使用File.Create创建再复制/移动/删除时会提示:文件正由另一进程使用,因此该进程无法访问该文件
//
改用 FileStream 获取 File.Create 返回的 System.IO.FileStream 再进行关闭就无此问题
FileStream fs;
fs
=
File.Create(Server.MapPath(
"
a.txt
"
));
fs.Close();
fs
=
File.Create(Server.MapPath(
"
b.txt
"
));
fs.Close();
fs
=
File.Create(Server.MapPath(
"
c.txt
"
));
fs.Close();
//
复制文件
File.Copy(Server.MapPath(
"
a.txt
"
), Server.MapPath(
"
a\\a.txt
"
));
//
移动文件
File.Move(Server.MapPath(
"
b.txt
"
), Server.MapPath(
"
a\\b.txt
"
));
File.Move(Server.MapPath(
"
c.txt
"
), Server.MapPath(
"
a\\c.txt
"
));
//
删除文件
File.Delete(Server.MapPath(
"
a.txt
"
));
//
3.遍历文件夹中的文件和子文件夹并显示其属性---------
if
(Directory.Exists(Server.MapPath(
"
a
"
)))
{
//
所有子文件夹
foreach
(
string
item
in
Directory.GetDirectories(Server.MapPath(
"
a
"
)))
{
Response.Write(
"
<b>文件夹:
"
+
item
+
"
</b><br/>
"
);
DirectoryInfo directoryinfo
=
new
DirectoryInfo(item);
Response.Write(
"
名称:
"
+
directoryinfo.Name
+
"
<br/>
"
);
Response.Write(
"
路径:
"
+
directoryinfo.FullName
+
"
<br/>
"
);
Response.Write(
"
创建时间:
"
+
directoryinfo.CreationTime
+
"
<br/>
"
);
Response.Write(
"
上次访问时间:
"
+
directoryinfo.LastAccessTime
+
"
<br/>
"
);
Response.Write(
"
上次修改时间:
"
+
directoryinfo.LastWriteTime
+
"
<br/>
"
);
Response.Write(
"
父文件夹:
"
+
directoryinfo.Parent
+
"
<br/>
"
);
Response.Write(
"
所在根目录:
"
+
directoryinfo.Root
+
"
<br/>
"
);
Response.Write(
"
<br/>
"
);
}
//
所有子文件
foreach
(
string
item
in
Directory.GetFiles(Server.MapPath(
"
a
"
)))
{
Response.Write(
"
<b>文件:
"
+
item
+
"
</b><br/>
"
);
FileInfo fileinfo
=
new
FileInfo(item);
Response.Write(
"
名称:
"
+
fileinfo.Name
+
"
<br/>
"
);
Response.Write(
"
扩展名:
"
+
fileinfo.Extension
+
"
<br/>
"
);
Response.Write(
"
路径:
"
+
fileinfo.FullName
+
"
<br/>
"
);
Response.Write(
"
大小:
"
+
fileinfo.Length
+
"
<br/>
"
);
Response.Write(
"
创建时间:
"
+
fileinfo.CreationTime
+
"
<br/>
"
);
Response.Write(
"
上次访问时间:
"
+
fileinfo.LastAccessTime
+
"
<br/>
"
);
Response.Write(
"
上次修改时间:
"
+
fileinfo.LastWriteTime
+
"
<br/>
"
);
Response.Write(
"
所在文件夹:
"
+
fileinfo.DirectoryName
+
"
<br/>
"
);
Response.Write(
"
文件属性:
"
+
fileinfo.Attributes
+
"
<br/>
"
);
Response.Write(
"
<br/>
"
);
}
}
//
4.文件读写---------
if
(File.Exists(Server.MapPath(
"
a\\a.txt
"
)))
{
StreamWriter streamwrite
=
new
StreamWriter(Server.MapPath(
"
a\\a.txt
"
));
streamwrite.WriteLine(
"
木子屋
"
);
streamwrite.WriteLine(
"
http://www.mzwu.com/
"
);
streamwrite.Write(
"
2008-04-13
"
);
streamwrite.Close();
StreamReader streamreader
=
new
StreamReader(Server.MapPath(
"
a\\a.txt
"
));
Response.Write(streamreader.ReadLine());
Response.Write(streamreader.ReadToEnd());
streamreader.Close();
}
查看全文
相关阅读:
void类型和void *指针类型(网上摘抄总结)【转】
微信小程序中的canvas基础应用
Google Interview University 一套完整的学习手册帮助自己准备 Google 的面试
git使用教程(详细)
Vue下载依赖遇到的一些bug
原生Javascript客户端表单验证
ASP MVC 后台控制器弹出框提示
网页URLJs传值传值传值传值传值问题
从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑
弹出提示框,在跳转
原文地址:https://www.cnblogs.com/zhangpengshou/p/1351481.html
最新文章
python set 集合操作
python dict字典常用操作
利用原生JS实时监听input框输入值
angularjs ng-if ng-show ng-hide区别
AngularJS自定义指令之可选参数replace
angular 自定义指令详解 Directive
数据库 mysql 操作
AJAX请求和普通HTTP请求区别
http协议
Vue.js---配置开发环境
热门文章
readyState xhr对象当前状态
valid No such filter: 'drawtext'"
VS2010关于调用ffmpeg借口出错
如何查看ffmpeg支持的编码器和封装格式
FFmpeg 基本用法
c/c++通用内存泄漏检测框架GMFD(General Memory Fault Detection Framework)
深入理解c语言——‘ ’ ,‘0’, “0” ,0之间的区别
windows迁移linux问题集锦
ffmpeg av_interleaved_write_frame Operation not permitted
指向对象的指针
Copyright © 2011-2022 走看看