C# 针对文件夹的操作
//创建文件夹
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();
}
获取文件的版本信息:
FileVersionInfo myFileVersionInfo1 = FileVersionInfo.GetVersionInfo("D:\\TEST.DLL");
textBox1.Text="版本号: " + myFileVersionInfo1.FileVersion;
更改文件属性,删除只读文件:
下例欲将E:\test.txt文件拷贝至D:\tmp\test.txt,但D:\tmp\test.txt已经存在。
//File.Copy(sourceFile,destinationFile,true); 用来拷贝文件
//当destinationFile已经存在时,无法将文件file1拷贝到目标文件,
//因此先删除destination文件,File.Delete()方法不能删除只读文件,
//因此,如果文件属性为只读(Attributes属性中会包含有"ReadOnly"),
//先把文件属性重置为Normal,然后再删除:
string file1="E:\\test.txt";
string destinationFile="d:\\tmp\\test.txt";
if(File.Exists(destinationFile))
{
FileInfo fi=new FileInfo(destinationFile);
if(fi.Attributes.ToString().IndexOf("ReadOnly")!=-1)
fi.Attributes=FileAttributes.Normal;
File.Delete(destinationFile);
}
File.Copy(file1,destinationFile,true);
判断文件是否存在:File.Exists(string filePath)
判断目录是否存在:Directory.Exists("D:\\LastestVersion")
按行读取文件:
int fileCount=0;
// Open the file just specified such that no one else can use it.
StreamReader sr = new StreamReader(textBox1.Text.Trim());
while(sr.Peek() > -1)//StreamReader.Peek()返回下一个可用字符,但不使用它
{
listBox1.Items.Add(sr.ReadLine());
fileCount++;
}
sr.Close();
按行写入文件:
StreamWriter sw = new StreamWriter("D:\\result.txt");
for(int i=0;i<10;i++)
{
sw.WriteLine("这是第"+i.ToString()+"行数据");
}
C#追加文件
StreamWriter sw = File.AppendText(Server.MapPath(".")+"file://mytext.txt/");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET笔记");
sw.Flush();
sw.Close();
C#拷贝文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"file://mytext.txt/";
NewFile = Server.MapPath(".")+"file://mytextcopy.txt/";
File.Copy(OrignFile,NewFile,true);
C#删除文件
string delFile = Server.MapPath(".")+"file://mytextcopy.txt/";
File.Delete(delFile);
C#移动文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"file://mytext.txt/";
NewFile = Server.MapPath(".")+"file://mytextcopy.txt/";
File.Move(OrignFile,NewFile);
C#创建目录
// 创建目录c:\sixAge
DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");
// d1指向c:\sixAge\sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 将当前目录设为c:\sixAge
Directory.SetCurrentDirectory("c:\\sixAge");
// 创建目录c:\sixAge\sixAge2
Directory.CreateDirectory("sixAge2");
// 创建目录c:\sixAge\sixAge2\sixAge2_1
Directory.CreateDirectory("sixAge2\\sixAge2_1");
递归删除文件夹及文件
<%@ Page Language=C#%>
<%@ Import namespace="System.IO"%>
<Script runat=server>
public void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach(string d in Directory.GetFileSystemEntries(dir))
{
if(File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir); //删除已空文件夹
Response.Write(dir+" 文件夹删除成功");
}
else
Response.Write(dir+" 该文件夹不存在"); //如果文件夹不存在则提示
}
protected void Page_Load (Object sender ,EventArgs e)
{
string Dir="D:\\gbook\\11";
DeleteFolder(Dir); //调用函数删除文件夹
}
C# 针对文件夹的操作的更多相关文章
- java io流 对文件夹的操作
java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...
- python之文件的读写和文件目录以及文件夹的操作实现代码
这篇文章主要介绍了python之文件的读写和文件目录以及文件夹的操作实现代码,需要的朋友可以参考下 为了安全起见,最好还是给打开的文件对象指定一个名字,这样在完成操作之后可以迅速关闭文件,防止一些无用 ...
- Qt: 文件、文件夹的操作;
Qt没有提供单独的函数来对文件.文件夹进行操作, 但是提供了两个类: QFile, QDir; 1.文件操作 ) 文件是否存在: QFile file("D:/test.jpg") ...
- JAVA文件操作类和文件夹的操作代码示例
JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...
- python 关于文件夹的操作
在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() # path 表示文 ...
- java文件夹相关操作 演示样例代码
java文件夹相关操作 演示样例代码 package org.rui.io; import java.io.File; import java.io.FilenameFilter; import ja ...
- java通过sftp对linux服务器文件夹进行操作
本文主要讲sftp对linux服务器的文件和文件夹进行操作,windows server 服务器不支持. package com.lx.ftp; import java.io.File; import ...
- C# 对文件与文件夹的操作包括删除、移动与复制
在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类.文件夹(Folder)是只在Wind ...
- SFTP上传下载文件、文件夹常用操作
SFTP上传下载文件.文件夹常用操作 1.查看上传下载目录lpwd 2.改变上传和下载的目录(例如D盘):lcd d:/ 3.查看当前路径pwd 4.下载文件(例如我要将服务器上tomcat的日志文 ...
随机推荐
- Python学习-算术运算符,赋值运算符和复合运算符
算术运算符 常见的算术运算符有 : + 加法运算符 print(1 + 2); // 3 print('1' + '2'); //12 不仅可以进行2个数字的相加,还可以连接2个字符串 - ...
- 吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个javabean之间的相互拷贝,自己写的就当是研究咯---https://www.cnblogs.com/NieXiaoHui/p/7150928.html
吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个ja ...
- spring历史地址
http://repo.spring.io/release/org/springframework/spring/
- PatentTips - Method for booting a host device from an MMC/SD device
FIELD OF THE INVENTION The present invention relates to a memory device and especially to the interf ...
- Nginx 重写规则指南1
作者:运维生存时间 - 默北 链接:www.ttlsa.com/nginx/nginx-rewriting-rules-guide/ 当运维遇到要重写情况时,往往是要程序员把重写规则写好后,发给你,你 ...
- Lotto(DFS处理)
题目再现 题目内容: 给定N个数字,再从中选定M个数字出来. 将每一种组合内的数字由小到大排列之后, 将全部组合依照字典序排列. 请你找出第X组的第Y个数字. 给定的数字为1~N. 范例1 (N,M, ...
- [Vue @Component] Control Template Contents with Vue's Render Function
Declaring templates and elements inside of templates works great for most scenarios. Sometimes you n ...
- VFL演示样例
上篇文章向大家介绍了VFL的基本的语法点,假设对下面演示样例不熟的童鞋,能够前去參考.废话不多说.我们直接来看演示样例. 演示样例一 将五个大小同样.颜色不同的view排成一行,view间的间隔为15 ...
- ios block 内存管理时使用注意
XMGStudent *stu = [[XMGStudent alloc] init]; __weak XMGStudent *weakStu = stu; stu.block = ^{ NSLog( ...
- Cocos2d-x 3.x 图形学渲染系列十五
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家.特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...