//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 操作的更多相关文章

  1. Java文件File操作一:文件的创建和删除

    一.简述 File 文件类,主要对文件进行相关操作.常用的File操作有:文件(夹)的创建.文件(夹)的删除,文件的读入和下载(复制)等: 二.文件(夹)的创建和删除 1.创建过程 实例: //cre ...

  2. python学习笔记3---浅拷贝和深拷贝,file操作

    import copy a=[1,2,3,['a','b']] b=a c= copy.copy(a)---浅拷贝 d=copy.deepcopy(a)---深拷贝 file操作: python 文件 ...

  3. 【转载】Java File操作汇总

    转载自博客:https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F 本文通过大量的示例, ...

  4. phonegap file操作

    phonegap中,有时候需要操作到手机中的文件,这个时候就需要用到phonegap官方提供的插件 file ,英文好的可以直接参考官方文档 首先是安装插件:(需要phonegap 3.0 以上,不止 ...

  5. [Python Study Notes] Basic I\O + File 操作

    列表操作 Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...

  6. Java-IO流之File操作和Properties操作

    java的File类主要是用来操作文件的元数据,稍作演示如下: 其中方法getAllJavaFile()是使用了过滤器FileFileter,这个过滤器只需要实现accept方法,判断什么样的文件返回 ...

  7. Java 学习笔记 IO流与File操作

    可能你只想简单的使用,暂时不想了解太多的知识,那么请看这里,了解一下如何读文件,写文件 读文件示例代码 File file = new File("D:\\test\\t.txt" ...

  8. File操作

    对文件进行操作(只操作小文件) bool Exists(string path) 判断文件是否存在 FileStream Create(string path) 创建文件 void Move(stri ...

  9. C#常用操作类库四(File操作类)

    public class FileHelper : IDisposable { private bool _alreadyDispose = false; #region 构造函数 public Fi ...

随机推荐

  1. Android图片处理-相机、相处简单调用

    安卓开发中,常常需要使用到手机相机拍照.或者相册上传头像等等.通过使用Intent,我们很方便地获得相机.相册里面的图片: 1.相机调用,通过设置File文件路径和文件名,可以将拍照得到的图片保存下来 ...

  2. Android——GridView(网格视图)相关知识总结贴

    Android API中文文档GridView http://www.apkbus.com/android-14131-1-1.html   Android API 中文 (15) —— GridVi ...

  3. Android SDK下载和更新失败的解决方法【转】

    启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口:在『Androi ...

  4. JAX-RS(基于Jersey) + Spring 4.x + MyBatis构建REST服务架构

    0. 大背景 众所周知,REST架构已经成为现代服务端的趋势. 很多公司,已经采用REST作为App, H5以及其它客户端的服务端架构. 1. 什么是JAX-RS? JAX-RS是JAVA EE6 引 ...

  5. vim 多行注释消除注释,多行删除

    进入可视化模式: Ctrl+v 继续进入编辑模式: shift+i 注释: shift+# 注释生效: ESC 取消注释 d 删除 选中全部字符块区域,使用方向键上下右: 然后,按一下d

  6. html5media.js 让浏览器兼容<Video><Audio> 标签

    介绍:https://html5media.info/ 项目:https://github.com/etianen/html5media Wiki:https://github.com/etianen ...

  7. 【Linux】——sleep无法正常休眠

    最近在开发项目的时候遇到一个问题,当使用 sleep(2) 的时候,程序居然没有按照指定的时间去休眠,但是连续执行两次 sleep(2) 的时候,程序可以正常的休眠 2 秒.真是见鬼了.最后查看了以下 ...

  8. Swift 关键字汇总

    常见的关键字有以下4种 与声明有关的关键字:class.deinit.enum.extension.func.import.init.let.protocol.static.struct.subscr ...

  9. 开发Android必知的工具

    程序开发有时候非常依赖使用的开发工具,好的完备的开发工具可以让开发人员的工作效率有大幅度的提高.开发Android也是如此,大家可能都离不开Eclipse或Android Studio这些工具,但他们 ...

  10. SQL 存储过程入门(事务)(四)

    SQL 存储过程入门(事务)(四)   本篇我们来讲一下事务处理技术. 为什么要使用事务呢,事务有什么用呢,举个例子. 假设我们现在有个业务,当做成功某件事情的时候要向2张表中插入数据,A表,B表,我 ...