C#代码 复制代码 收藏代码
using System.IO;
/// <summary>
/// 获取指定目录下的所有文件夹名
/// </summary>
/// <param name="path">目录路径</param>
/// <returns>string,返回所有文件夹名字</returns>
public string GetAllFolder(string path)
{
	string folder_Names = "";
	DirectoryInfo dir = new DirectoryInfo(path);

	foreach (DirectoryInfo subdir in dir.GetDirectories())
		folder_Names += subdir.FullName + ",";

	return folder_Names;
}



C#代码 复制代码 收藏代码
using System.IO;
/// <summary>
/// 获取指定目录下的所有文件和文件夹大小
/// </summary>
/// <param name="path">目录路径</param>
/// <returns>string,返回所有文件夹名字</returns>
protected long GetDirectorySize(string path)
{
	long dirSize = 0;
	DirectoryInfo dir = new DirectoryInfo(path);
	foreach (FileInfo file in dir.GetFiles())
		dirSize += file.Length;
	foreach (DirectoryInfo subdir in dir.GetDirectories())
		dirSize += GetDirectorySize(subdir.FullName);
	return dirSize;
}

静态生成要在虚拟目录下创建文件夹 来保存生成的页面 那么就要对文件进行操作

一、创建文件夹
        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>");
        }


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


    前台
    <asp:ListBox ID="list" runat="server" Width="200px" Height="300px" Visible="false"></asp:ListBox>
    后台
        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("移动失败!");
        }

八、文件删除
        string name = "bb\\1.html";
      string path = Server.MapPath("") + "\\" + name;
     
        if (!File.Exists(path))
        {
            Response.Write("<script>alert('文件" + name + "不存在!');history.go(-1);</script>");
            return;
        }
        try
        {
            File.Delete(path);
            Response.Write("删除成功!");
        }
        catch
        {
            Response.Write("删除失败!");
        }

九、获取文件的详细信息
string name = "aa\\11.txt";
      string path = Server.MapPath("") + "\\" + name;
      if (!File.Exists(path))
      {
          Response.Write("<script>alert('文件" + name + "不存在!');history.go(-1);</script>");
          return;
      }
      else
      {
          FileInfo file = new FileInfo(path);
          string filexinxi1, filexinxi2, filexinxi3, filexinxi4, filexinxi5;
          //文件路径
          filexinxi1 = file.FullName;
          //文件大小,字节
          filexinxi2 = file.Length+"字节";
          //文件属性
          filexinxi3 = file.Attributes.ToString();
          //文件创建时间
          filexinxi4 = file.CreationTime.ToShortDateString();
          //文件上次访问时间
          filexinxi5 = file.LastAccessTime.ToShortDateString();
          Response.Write("文件路径:"+filexinxi1+"<br>");
          Response.Write("文件大小:" + filexinxi2 + "<br>");
          Response.Write("文件属性:" + filexinxi3 + "<br>");
          Response.Write("文件创建时间:" + filexinxi4 + "<br>");
          Response.Write("文件上次访问时间:" + filexinxi5 + "<br>");
      }
十、读取文件内容

    string name = "aa\\11.html";
      string path = Server.MapPath("") + "\\" + name;
      FileInfo fi = new FileInfo(path);
      //获取文件名
      string filename = fi.Name;
        //获取文件扩展名
      string extension = fi.Extension;
        //判断该文件是否为txt格式
      if (extension != ".html")
      {
          Response.Write("<script>alert('请选择html格式文件!');history.go(-1);</script>");
          return;
      }
      StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);
      Response.Write("文件中的内容如下:<br>");
      Response.Write(sr.ReadToEnd());
      sr.Close();
十一、文件的写入
      string name = "aa\\11.html";
        string content1="<html><title>大家好</title></html>";
      string path = Server.MapPath("") + "\\" + name;
      FileInfo fi = new FileInfo(path);
      //获取文件名
      string filename = fi.Name;
        //获取文件扩展名
      string extension = fi.Extension;
        //判断该文件是否为txt格式
      if (extension != ".html")
      {
          Response.Write("<script>alert('请选择html格式文件!');history.go(-1);</script>");
          return;
      }
      StreamWriter sr = new StreamWriter(path, true, System.Text.Encoding.GetEncoding("gb2312"));
      sr.WriteLine(content1);
      sr.Close();
      Response.Write("文件写入成功!");

 

 #region 创建目录
        public static void FileCreate(string Path)
        {
            FileInfo CreateFile = new FileInfo(Path); //创建文件
            if (!CreateFile.Exists)
            {
                FileStream FS = CreateFile.Create();
                FS.Close();
            }
        }
        #endregion

        #region 递归删除文件夹目录及文件
        /****************************************
         * 函数名称:DeleteFolder
         * 功能说明:递归删除文件夹目录及文件
         * 参    数:dir:文件夹路径
         * 调用示列:
         *           string dir = Server.MapPath( "test/");
         *           EC.FileObj.DeleteFolder(dir);      
        *****************************************/
        /// <summary>
        /// 递归删除文件夹目录及文件
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static 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, true); //删除已空文件夹                
            }
        }

        #endregion

        #region 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
        /****************************************
         * 函数名称:CopyDir
         * 功能说明:将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
         * 参    数:srcPath:原始路径,aimPath:目标文件夹
         * 调用示列:
         *           string srcPath = Server.MapPath( "test/");
         *           string aimPath = Server.MapPath( "test1/");
         *           EC.FileObj.CopyDir(srcPath,aimPath);  
        *****************************************/
        /// <summary>
        /// 指定文件夹下面的所有内容copy到目标文件夹下面
        /// </summary>
        /// <param name="srcPath">原始路径</param>
        /// <param name="aimPath">目标文件夹</param>
        public static void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 判断目标目录是否存在如果不存在则新建之
                if (!Directory.Exists(aimPath))
                    Directory.CreateDirectory(aimPath);
                // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                //如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
                //string[] fileList = Directory.GetFiles(srcPath);
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                //遍历所有的文件和目录
                foreach (string file in fileList)
                {
                    //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件

                    if (Directory.Exists(file))
                        CopyDir(file, aimPath + Path.GetFileName(file));
                    //否则直接Copy文件
                    else
                        File.Copy(file, aimPath + Path.GetFileName(file), true);
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.ToString());
            }
        }
        #endregion