zoukankan      html  css  js  c++  java
  • C# IO操作,文件 文件夹 长沙

    一、创建文件夹

        using System.IO;  
                string name = "aa";  
                string path = Server.MapPath("") + "//" + name;  
                if (Directory.Exists(path))  
                {  
                Response.Write("<script>alert('文件夹已存在了!');history.go(-1);</script>");  
                }  
                else  
                {  
                DirectoryInfo folder=Directory.CreateDirectory(path);  
                string time = Convert.ToString(Directory.GetCreationTime(path));  
                string foldername = name.Substring(name.LastIndexOf("//") + 1);  
                Response.Write("添加成功!");  
                Response.Write("添加时间:"+time);  
                Response.Write("文件夹名:"+foldername);  
                }  

    二、删除文件夹

        using System.IO;  
                string name = "aa";  
                string path = Server.MapPath("") + "//" + name;  
                if (Directory.Exists(path))  
                {  
                    Directory.Delete(path);  
                    Response.Write("删除成功!");  
                }  
                else  
                {  
                Response.Write("<script>alert('文件夹不存在!');history.go(-1);</script>");  
                }  

    三、文件夹的移动

        string name1 = "aa";  
                string name2 = "bb//aa";      
              //移动到的文件夹里,把AA移动到BB里  
                string path1 = Server.MapPath("") + "//" + name1;  
                string path2 = Server.MapPath("") + "//" + name2;  
                if (!Directory.Exists(path1))  
                {  
                    Response.Write("<script>alert('文件夹"+name1+"不存在!');history.go(-1);</script>");  
                    return;  
                }  
                if (Directory.Exists(path2))  
                {  
                    Response.Write("<script>alert('文件夹" + name2 + "已存在!');history.go(-1);</script>");  
                    return;  
                }  
                try  
                {  
                    Directory.Move(path1, path2);  
                    Response.Write("文件夹移动成功!");  
                }  
                catch   
                {  
                    Response.Write("<script>alert('必须在同一目录下操作!');history.go(-1);</script>");  
                }  

    四、获取文件夹下的文件列表

    前台

        <aspistBox ID="list" runat="server" Width="200px" Height="300px" Visible="false"></aspistBox>  

    后台

        string name = "aa";  
                string path = Server.MapPath("") + "//" + name;  
                if (!Directory.Exists(path))  
                {  
                    list.Visible = false;  
                    Response.Write("<script>alert('文件夹" + name + "不存在!');history.go(-1);</script>");  
                    return;  
                }  
                else  
                {  
                    DirectoryInfo foldinfo = new DirectoryInfo(path);  
                    FileSystemInfo[] dirs = foldinfo.GetFileSystemInfos();  
                    if (dirs.Length < 1)  
                    {  
                        Response.Write("<script>alert('文件夹中没数据!');history.go(-1);</script>");  
                        return;  
                    }  
                    list.Visible = true;  
                    list.DataSource = dirs;  
                    list.DataBind();  
                }  

    五、创建文件

        string name = "aa.aspx";  
                string path = Server.MapPath("") + "//" + name;  
                if (File.Exists(path))  
                {  
                    Response.Write("<script>alert('文件" + name + "已存在!');history.go(-1);</script>");  
                    return;  
                }  
                else  
                {  
                    FileStream fs = File.Create(path);  
                    
                    fs.Close();  
                    Response.Write("文件" + name + "添加成功!");  
                }  

    六、拷贝文件

        string name1 = "aa//1.html";  
                string name2 = "bb//1.html";   
                string path1 = Server.MapPath("") + "//" + name1;  
                string path2 = Server.MapPath("") + "//" + name2;  
                if (!File.Exists(path1))  
                {  
                    Response.Write("<script>alert('文件" + name1 + "不存在!');history.go(-1);</script>");  
                    return;  
                }  
                if (File.Exists(path2))  
                {  
                    Response.Write("<script>alert('文件" + name2 + "已存在!');history.go(-1);</script>");  
                    return;  
                }  
                try  
                {  
                    File.Copy(path1, path2);  
                    Response.Write("拷贝成功!");  
                }  
                catch  
                {  
                    Response.Write("拷贝失败!");  
                }  

    七、移动文件

        string name1 = "aa//1.html";  
                string name2 = "bb//1.html";   
                string path1 = Server.MapPath("") + "//" + name1;  
                string path2 = Server.MapPath("") + "//" + name2;  
                if (!File.Exists(path1))  
                {  
                    Response.Write("<script>alert('文件" + name1 + "不存在!');history.go(-1);</script>");  
                    return;  
                }  
                if (File.Exists(path2))  
                {  
                    Response.Write("<script>alert('文件" + name2 + "已存在!');history.go(-1);</script>");  
                    return;  
                }  
                try  
                {  
                    File.Move(path1, path2);  
                    Response.Write("移动成功!");  
                }  
                catch  
                {  
                    Response.Write("移动失败!");  
                }  

    八、文件删除

     1     string name = "bb//1.html";  
     2           string path = Server.MapPath("") + "//" + name;  
     3             
     4             if (!File.Exists(path))  
     5             {  
     6                 Response.Write("<script>alert('文件" + name + "不存在!');history.go(-1);</script>");  
     7                 return;  
     8             }  
     9             try  
    10             {  
    11                 File.Delete(path);  
    12                 Response.Write("删除成功!");  
    13             }  
    14             catch  
    15             {  
    16                 Response.Write("删除失败!");  
    17             }  

    九、获取文件的详细信息

     1     string name = "aa//11.txt";  
     2          string path = Server.MapPath("") + "//" + name;  
     3          if (!File.Exists(path))  
     4          {  
     5              Response.Write("<script>alert('文件" + name + "不存在!');history.go(-1);</script>");  
     6              return;  
     7          }  
     8          else   
     9          {  
    10              FileInfo file = new FileInfo(path);  
    11              string filexinxi1, filexinxi2, filexinxi3, filexinxi4, filexinxi5;  
    12              //文件路径  
    13              filexinxi1 = file.FullName;  
    14              //文件大小,字节  
    15              filexinxi2 = file.Length+"字节";  
    16              //文件属性  
    17              filexinxi3 = file.Attributes.ToString();  
    18              //文件创建时间  
    19              filexinxi4 = file.CreationTime.ToShortDateString();  
    20              //文件上次访问时间  
    21              filexinxi5 = file.LastAccessTime.ToShortDateString();  
    22              Response.Write("文件路径:"+filexinxi1+"<br>");  
    23              Response.Write("文件大小:" + filexinxi2 + "<br>");  
    24              Response.Write("文件属性:" + filexinxi3 + "<br>");  
    25              Response.Write("文件创建时间:" + filexinxi4 + "<br>");  
    26              Response.Write("文件上次访问时间:" + filexinxi5 + "<br>");  
    27          }  

    十、读取文件内容

     1     string name = "aa//11.html";  
     2          string path = Server.MapPath("") + "//" + name;  
     3          FileInfo fi = new FileInfo(path);  
     4          //获取文件名  
     5          string filename = fi.Name;  
     6            //获取文件扩展名  
     7          string extension = fi.Extension;  
     8            //判断该文件是否为txt格式  
     9          if (extension != ".html")  
    10          {  
    11              Response.Write("<script>alert('请选择html格式文件!');history.go(-1);</script>");  
    12              return;  
    13          }  
    14          StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);  
    15          Response.Write("文件中的内容如下:<br>");  
    16          Response.Write(sr.ReadToEnd());  
    17          sr.Close();  

    十一、文件的写入

     1     string name = "aa//11.html";  
     2             string content1="<html><title>大家好</title></html>";  
     3           string path = Server.MapPath("") + "//" + name;  
     4           FileInfo fi = new FileInfo(path);  
     5           //获取文件名  
     6           string filename = fi.Name;  
     7             //获取文件扩展名  
     8           string extension = fi.Extension;  
     9             //判断该文件是否为txt格式  
    10           if (extension != ".html")  
    11           {  
    12               Response.Write("<script>alert('请选择html格式文件!');history.go(-1);</script>");  
    13               return;  
    14           }  
    15           StreamWriter sr = new StreamWriter(path, true, System.Text.Encoding.GetEncoding("gb2312"));  
    16           sr.WriteLine(content1);  
    17           sr.Close();  
    18           Response.Write("文件写入成功!");  
  • 相关阅读:
    redis集群报Jedis does not support password protected Redis Cluster configurations异常解决办法
    redis集群密码设置
    Redis 3.2.4集群实战
    Redis3.2.4 Cluster集群搭建
    redis集群环境的搭建和错误分析
    Centos iptables防火墙关闭启动详解
    动态的表格数据
    ubuntu使用抓包工具,charles
    layer结合art实现弹出功能
    禅道开源版源码安装
  • 原文地址:https://www.cnblogs.com/suqifeng/p/2910613.html
Copyright © 2011-2022 走看看