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 ...
随机推荐
- js 导出Excel
最近从Silverlight这边转到javascript过来,现在要导出一个导出excel的功能.上级领导指示当页显示多少数据,就导出多少数据,没有必要从后台在去数据.以前也没有接触过这方面的,在网上 ...
- python连接数据库
使用pymysql://安装pymysqlpip install pymysql 代码: # coding=utf8 import pymysql # 创建连接对象 conn = pymysql.co ...
- 关于C++单件模式释放对象
http://blog.csdn.net/windboyzsj/article/details/2790485 最近接触的一个项目要用到单件模式,我像往常一样哒哒(敲击键盘ing)一个单件模式的典型结 ...
- PHP 7問世,2億網站效能翻倍有望
經過10年的漫長等待,PHP 7終於正式問世了.這個影響全球8成網站的開發語言,一舉從5.0版,跳過了功敗垂成的6.0版,一舉進入了7.0時代. 20年前初夏,1995年6月8日,一位愛解決問題的C語 ...
- Advanced Puppet 系列的前言
什么是Advanced 在网络上,你能找到大量关于Puppet的安装,配置以及基础用法的文章和博客.你在通过一段时间的实战后,熟练掌握了Puppet基础用法,随着你管理的集群日益扩大,你的部署逻辑越来 ...
- 搞了个基于zookeeper的Leader/Follower切换Demo
基于zookeeper写了个Leader选举类库demo,场景如下: 上图中的Program1..4可以部署在1台server上,也可以部署在多台server上,也可以是一个进程中的多个线程. 运行效 ...
- Ay.Framework.WPF 2.0建立项目到底有多快
2015-3-31 今天我已经优化了很多地方,让客户使用起来几乎是傻瓜式使用了,废话不多说,我们开始吧. 默认的我提供了一些图片,但是也只占用了8M多,2.0版本目前总共有45M左右大小,毕竟包含了f ...
- SSH使用教程( Bitvise Tunnelier+Chrome+Proxy Switchy)
前言 网上很多讲解使用Bitvise Tunnelier+Chrome+Proxy Switchy进行SSHFQ操作的教材有所缺失的部分,不太全面,这里重新整理. 本篇博客的主要内容如下: 准备工作 ...
- ubuntu下安装Node.js(源码安装)
最近使用hexo的过程中出现了问题,中间载nodejs安装的时候也耽误了些许时间,所以在此记录一下安装的过程. 环境:ubuntu14.0.4LTS,安装nodejs版本node-v0.10.36.t ...
- benchmark pm2的cluster模式发布web app的性能与相关问题解决方法
pm2以cluster集群方式发布app,可以高效地利用多核cpu,有效提升吞吐量.在上周对公司的redmine服务器进行性能调优后,深感ruby on rails的性能低下,这次测试nodejs的s ...