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();
}

下篇

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Default3 : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
         ShowPic.Visible = false;//初始化不显示
         ShowText.Visible = false;//初始化不显示
     }
     protected void Button1_Click(object sender, EventArgs e)
     {

         if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)//如果不存在就创建file文件夹
         {
             Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));
         }

         //Directory.Delete(Server.MapPath("~/upimg/hufu"), true);//删除文件夹以及文件夹中的子目录,文件   

         //判断文件的存在

         if (File.Exists(Server.MapPath("~/upimg/Data.html")))
         {
             Response.Write("Yes");

             //存在文件

         }

         else
         {
             Response.Write("No");
             //不存在文件
             File.Create(MapPath("~/upimg/Data.html"));//创建该文件

         }

         string name = GetFiles.FileName;//获取已上传文件的名字
         string size = GetFiles.PostedFile.ContentLength.ToString();//获取已上传文件的大小
         string type = GetFiles.PostedFile.ContentType;//获取已上传文件的MIME
         string postfix = name.Substring(name.LastIndexOf(".") + 1);//获取已上传文件的后缀
         string ipath = Server.MapPath("upimg") +"\\"+ name;//获取文件的实际路径
         string fpath = Server.MapPath("upfile") + "\\" + name;
         string dpath = "upimg\\" + name;//判断写入数据库的虚拟路径

         ShowPic.Visible = true;//激活
         ShowText.Visible = true;//激活

         //判断文件格式
         if (name == "") { 
           Response.Write("<script>alert('上传文件不能为空')</script>");
         }

         else{

             if (postfix == "jpg" || postfix == "gif" || postfix == "bmp" || postfix == "png")
             {
                 GetFiles.SaveAs(ipath);
                 ShowPic.ImageUrl = dpath;
                 ShowText.Text = "你上传的图片名称是:" + name + "<br>" + "文件大小:" + size + "KB" + "<br>" + "文件类型:" + type + "<br>" + "存放的实际路径为:" + ipath;

             }

             else
             {
                 ShowPic.Visible = false;//隐藏图片
                 GetFiles.SaveAs(fpath);//由于不是图片文件,因此转存在upfile这个文件夹
                 ShowText.Text = "你上传的文件名称是:" + name + "<br>" + "文件大小:" + size + "KB" + "<br>" + "文件类型:" + type + "<br>" + "存放的实际路径为:" + fpath;

             }
        
         }
     }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值