zoukankan      html  css  js  c++  java
  • 一个文件夹操作类

    //杨春
    //2008-7-10
    //删除指定目录下所有文件   用来处理饼状图  柱状图产生的缓冲文件

    using System;
    using System.Data;
    using System.IO;
    using System.Data.SqlClient;

    /// <summary>
    /// DeleteFile 的摘要说明
    /// </summary>
    namespace YangChun
    {
        public class DeleteFile
        {
            public DeleteFile()
            {
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }

            /// <summary>
            /// 根据文件路径删除文件
            /// </summary>
            /// <param name="path">文件名(路径)</param>
            /// <returns>是否成功</returns>
            public static bool DeleteSigleFile(string path)
            {
                FileInfo file = new FileInfo(path);
                try
                {
                    if (file.Exists)
                    {
                        file.Delete();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    return false;
                }
            }

            /// <summary>
            /// 删除指定目录和目录下所有文件
            /// </summary>
            /// <param name="path">目录路径</param>
            /// <returns>是否成功</returns>
            public static bool DeleteDirectory(string path)
            {
                FileInfo info = new FileInfo(path);
                if (info.Exists)
                {
                    return DeleteSigleFile(path);
                }
                else
                {
                    DirectoryInfo directory = new DirectoryInfo(path);
                    try
                    {
                        if (directory.Exists != true)
                        {
                            return false;
                        }
                        if (directory.GetFiles().Length < 1)
                        {
                            directory.Delete();
                            return true;
                        }
                        else
                        {
                            foreach (FileInfo file in directory.GetFiles())
                            {
                                string filepath = file.FullName;
                                if (DeleteSigleFile(filepath) != true)
                                {
                                    return false;
                                }
                            }
                            directory.Delete();
                            return true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        return false;
                    }
                }
            }

            /// <summary>
            /// 根据路径删除该目录下所有文件(不包含该目录)
            /// </summary>
            /// <param name="path">路径</param>
            /// <returns>是否删除成功</returns>
            public static bool DeleteAllFileInDirectory(string path)
            {
                FileInfo file = new FileInfo(path);
                if (file.Exists)
                {
                    return DeleteSigleFile(path);
                }
                else
                {
                    DirectoryInfo directory = new DirectoryInfo(path);
                    try
                    {
                        if (directory.Exists != true)
                        {
                            return false;
                        }
                        if (directory.GetFiles().Length < 1)
                        {
                            return true;
                        }
                        else
                        {
                            foreach (FileInfo info in directory.GetFiles())
                            {
                                string filePath = info.FullName;
                                if (DeleteSigleFile(filePath) != true)
                                {
                                    return false;
                                }
                            }
                            return true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        return false;
                    }
                }
               
            }

            /// <summary>
            /// 得到文件夹大小
            /// </summary>
            /// <param name="path">路径</param>
            /// <returns>大小</returns>
            public static long GetDirectoryLength(string path)
            {
                long length = 0;
                if (Directory.Exists(path))
                {
                    DirectoryInfo directory = new DirectoryInfo(path);

                    foreach (FileInfo file in directory.GetFiles())
                    {
                        length += file.Length;
                    }
                    DirectoryInfo[] directoryInfo = directory.GetDirectories();
                    if (directoryInfo.Length > 0)
                    {
                        for (int i = 0; i < directoryInfo.Length; i++)
                        {
                            length = GetDirectoryLength(directoryInfo[i].FullName);
                        }
                    }
                    return length;
                }
                else
                {
                    return length;
                }
            }

            /// <summary>
            /// 根据表名取得该表记录行数
            /// </summary>
            /// <param name="tableName">表名</param>
            /// <returns>记录行数</returns>
            public static int GetTableRecordCount(string tableName)
            {
                int result = 0;
                string sql = "SELECT COUNT(0) AS tableCount FROM " + tableName;
                SqlConnection connection = new SqlConnection(DAL.DbHelperSQL.GetConnectionString());
                SqlCommand cmd = new SqlCommand(sql, connection);
                try
                {
                    connection.Open();
                    result = (int)cmd.ExecuteScalar();
                    return result;
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    return 0;
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }
                   
            }

            /// <summary>
            /// 根据表名删除该表所有记录
            /// </summary>
            /// <param name="tableName">表名</param>
            /// <returns>操作是否成功</returns>
            public static bool DeleteTableRecord(string tableName)
            {
                string sql = "DELETE FROM " + tableName;
                SqlConnection connection = new SqlConnection(DAL.DbHelperSQL.GetConnectionString());
                SqlCommand cmd = new SqlCommand(sql, connection);
                try
                {
                    connection.Open();
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    return false;
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }
            }
        }
    }

  • 相关阅读:
    关于DISCUZ!NT发布的问题整理!
    javascript 文字滚动显示[zhuan ]
    Bot Framework Emulator应用与调试
    Bot Framework的简单实现
    命名空间在扩展方法中的妙用
    Error:Execution failed for task ':app:transformClassesWithDexForDebug"
    C#中WebApi接口传参不再困惑:传参详解
    贝塞尔曲线
    从枚举值获取对应的文本描述
    常用正则表达式收集
  • 原文地址:https://www.cnblogs.com/88223100/p/1241275.html
Copyright © 2011-2022 走看看