转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> C语言系列 >> 正文
C# 文件创建、移动、删除、复制         ★★★

C# 文件创建、移动、删除、复制

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1121 更新时间:2012/4/11 16:43:46

C# 文件创建、移动、删除、复制 

以下是代码片段:
//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#追加文件
StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET笔记");
sw.Flush();
sw.Close();

C#拷贝文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);

C#删除文件
string delFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Delete(delFile);

C#移动文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Move(OrignFile,NewFile);

C#创建目录
// 创建目录c:\sixAge
DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");
// d1指向c:\sixAge\sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 将当前目录设为c:\sixAge
Directory.SetCurrentDirectory("c:\\sixAge");
// 创建目录c:\sixAge\sixAge2
Directory.CreateDirectory("sixAge2");
// 创建目录c:\sixAge\sixAge2\sixAge2_1
Directory.CreateDirectory("sixAge2\\sixAge2_1");

递归删除文件夹及文件
<%@ Page Language=C#%>
<%@ Import namespace="System.IO"%>
<Script runat=server>
public void DeleteFolder(string dir)
{
    if (Directory.Exists(dir)) //如果存在这个文件夹删除之
    {
        foreach(string d in Directory.GetFileSystemEntries(dir))
        {
            if(File.Exists(d))
                File.Delete(d); //直接删除其中的文件
            else
                DeleteFolder(d); //递归删除子文件夹
        }
        Directory.Delete(dir); //删除已空文件夹
        Response.Write(dir+" 文件夹删除成功");
    }
    else
        Response.Write(dir+" 该文件夹不存在"); //如果文件夹不存在则提示
}

protected void Page_Load (Object sender ,EventArgs e)
{
    string Dir="D:\\gbook\\11";
    DeleteFolder(Dir); //调用函数删除文件夹
}


[C语言系列]C#注册表的读,写,删除,查找  [Web开发]asp函数:创建数据表/创建列/添加字段/修改字段/A…
[Web开发]ASP添加、删除、修改、数据表SQL语句  [办公软件]批量删除Office文档(word,excle,powerpoint)中的超…
[办公软件]如何删除PowerPoint幻灯片中的页脚信息  [办公软件]如何在PowerPoint演示文稿中插入(增加)新幻灯片、…
[办公软件]如何在PowerPoint中插入艺术字、修改艺术字、删除…  [办公软件]PowerPoint XP技巧之实现字幕上下移动动画
[办公软件]在幻灯片中应用设计模版无法复制图片的解决方案  [办公软件]利用幻灯片搜索器高效、快速批量复制另外一个演示…
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台