//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. 重学JAVA基础(七):线程的wait、notify、notifyAll、sleep

    /** * 测试thread的wait notify notifyAll sleep Interrupted * @author tomsnail * @date 2015年4月20日 下午3:20: ...

  2. SQL问题集锦

    1.union和union all的区别:http://www.cnblogs.com/xiangshu/articles/2054447.html

  3. SVN命令模式批量更新多个项目文件

    使用svn作为版本管理是,在一个仓库下边同时建立多个项目,每天上班都需要一个个更新,为了发挥程序员懒的精神,能让电脑做的,绝不手工操作.作为自动化处理,在windows环境,首先想到了bat Tort ...

  4. 导出特定内容成insert语句

           )                                EXEC('SELECT ' + @insert_sql + ' FROM ' + @table  )

  5. 实用的插件:跨浏览器复制jQuery-zclip

    Query-zclip是一个复制内容到剪贴板的jQuery插件,使用它我们不用考虑不同浏览器和浏览器版本之间的兼容问题.jQuery-zclip插件需要Flash的支持,使用时记得安装Adobe Fl ...

  6. 【C++沉思录】代理类

    1.考虑下面的场景:设计一个容器,包含一组类型不同但相互关联的对象(比如:Animal,Dog,Cat),对象具备多态行为.2.容器一般只能包含一种类型的对象,使用vector<Animal&g ...

  7. quick -- 创建精灵和动作

    local imgBg = display.newSprite("666666.jpg") :pos(display.cx, display.cy) :addTo(self) , ...

  8. 8 个最优秀的 Android Studio 插件

    Android Studio是目前Google官方设计的用于原生Android应用程序开发的IDE.基于JetBrains的IntelliJ IDEA,这是Google I/O 2013第一个宣布的作 ...

  9. cordova plugin数据传递概要

    cordova plugin数据传递概要: 1.调用pluginManager向所有插件发送消息: PluginManager.postMessage(String id, Object data); ...

  10. (笔记)Linux内核学习(九)之内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...