//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. CreateJSのeasel.js(一)

    CreateJS是基于HTML5开发的一套模块化的库和工具. 基于这些库,可以非常快捷地开发出基于HTML5的游戏.动画和交互应用. CreateJS为CreateJS库,可以说是一款为HTML5游戏 ...

  2. Leetcode-122 Best Time to Buy and Sell Stock II

    #122  Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...

  3. 改善C#公共程序类库质量的10种方法

    最近重构一套代码,运用以下几种方法,供参考. 1  公共方法尽可能的使用缓存 public static List<string> GetRegisteredCompany() { Str ...

  4. 通过weburl 启动windows程序

    1. 注册表修改 建立一个reg文件 执行导入  以RunLocal协议为例子 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\RunL ...

  5. td 自动换行

    Two solutions for cell width:1. Omit words: <td style="width:60px;"><div style=&q ...

  6. Android UI系列-----时间、日期、Toasts和进度条Dialog

    您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...

  7. win7下虚拟机安装mac 转载自 http://itbbs.pconline.com.cn/50602805.html

    最近,不断有人问起,如何在vmware下安装MAC系统.起因是以前曾发过一篇贴,在vmware8下安装MAC的方法.于是,重新下载了最新版苹果系统10.8.5,终于成功安装.现将注意事项及过程与各位朋 ...

  8. count有关

    1.count有两个作用:统计某个字段有值的记录数:统计结果集的记录数.2.count括号内的表达式不为null,就是统计结果集的记录数.也就是说,count(1),count(*),count(10 ...

  9. WebRTC is for Losers:WebRTC是输家

    该文章是引述,仅代表作者Dave Michels观点 WebRTC is for Losers WebRTC technology has fallen short on many of its pr ...

  10. A/B测试

    昨天把前段时间开发的二胡调音器的应用发布到了亚马逊应用程序商店,看到了一个A/B测试的标签,了解一下A/B测试的工作原理. A/B测试是一种新兴的网页优化方法,可以用于增加转化率注册率等网页指标. 使 ...