c# File 操作
//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();
}
获取文件的版本信息:
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# File 操作的更多相关文章
- Java文件File操作一:文件的创建和删除
一.简述 File 文件类,主要对文件进行相关操作.常用的File操作有:文件(夹)的创建.文件(夹)的删除,文件的读入和下载(复制)等: 二.文件(夹)的创建和删除 1.创建过程 实例: //cre ...
- python学习笔记3---浅拷贝和深拷贝,file操作
import copy a=[1,2,3,['a','b']] b=a c= copy.copy(a)---浅拷贝 d=copy.deepcopy(a)---深拷贝 file操作: python 文件 ...
- 【转载】Java File操作汇总
转载自博客:https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F 本文通过大量的示例, ...
- phonegap file操作
phonegap中,有时候需要操作到手机中的文件,这个时候就需要用到phonegap官方提供的插件 file ,英文好的可以直接参考官方文档 首先是安装插件:(需要phonegap 3.0 以上,不止 ...
- [Python Study Notes] Basic I\O + File 操作
列表操作 Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...
- Java-IO流之File操作和Properties操作
java的File类主要是用来操作文件的元数据,稍作演示如下: 其中方法getAllJavaFile()是使用了过滤器FileFileter,这个过滤器只需要实现accept方法,判断什么样的文件返回 ...
- Java 学习笔记 IO流与File操作
可能你只想简单的使用,暂时不想了解太多的知识,那么请看这里,了解一下如何读文件,写文件 读文件示例代码 File file = new File("D:\\test\\t.txt" ...
- File操作
对文件进行操作(只操作小文件) bool Exists(string path) 判断文件是否存在 FileStream Create(string path) 创建文件 void Move(stri ...
- C#常用操作类库四(File操作类)
public class FileHelper : IDisposable { private bool _alreadyDispose = false; #region 构造函数 public Fi ...
随机推荐
- Java插件开发-取插件下的某个文件
//找到插件所在处 Bundle bundle = Activator.getDefault().getBundle(); //根据插件转义成URL路径 URL url = FileLocator.t ...
- python多线程生成缩略图
在img目录下7张图片 分别是 11.jpg 22.jpg 33.jpg 44.jpg 55.jpg 66.jpg 77.jpg #encoding=utf-8 import os import ti ...
- python判断文件和目录是否存在
#Python的os.path模块提供了 isdir() 和 isfile()函数,请导入该模块,并调用函数判断指定的目录和文件是否存在. import os print os.path.isdir( ...
- [原创]移动应用测试技术圈QQ群:211828629
[原创]移动应用测试技术圈QQ群:211828629 移动应用测试技术圈QQ群:211828629,研究ios,android,winphone等平台测试技术,涉及功能/性能/安全/自动化/用户体验 ...
- 解决android SwipeRefreshLayout recyclerview 不能下拉
http://stackoverflow.com/questions/25178329/recyclerview-and-swiperefreshlayout 23down vote write th ...
- XMPP系列2:如何掌握XMPP协议
michaely 回答于 2012-08-07 08:34 举报我要说的是:1.任何一个协议想学习并熟练掌握,都不是一天两天的事情.2.XMPP协议现在已经有很多成熟的架构和客户端,无需重新造轮子.3 ...
- [C] tcharall(让所有平台支持TCHAR)v1.1。源码托管到github、添加CMake编译配置文件、使用doxygen规范注释
作者:zyl910 v1.1版的改动如下—— 将源码上传到github. 调整目录结构. 添加CMake编译配置文件. 使用doxygen规范注释. 文件清单—— docs\ docs\images\ ...
- 调试X Server
发现错误 运行X之后出现了错误: Program received signal SIGSEGV, Segmentation fault. 0x00007fbc3336fb63 in ?? () fr ...
- Unity 由Verlet数值积分产生的头发运动
先发下效果图. 参考项目unitychan-crs-master与miloyip大神的博客 爱丽丝的发丝,使用Verlet数值积分,根据旧的现在位置与上一桢位置来计算现在的位置,得到新的方向,上面的运 ...
- Linux 导入epel源
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh http:// ...