• 一个文件夹操作类


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

  • 相关阅读:
    Qt之QLineEdit
    Redis与Java
    Qt之属性系统
    Qt之Meta-Object系统
    设计模式(八)外观模式
    Qt之QRadioButton
    Qt之QSystemTrayIcon
    Redis与Java
    JSP公用COMMON文件
    eclipse远程调试Tomcat方法[转]
  • 原文地址:https://www.cnblogs.com/88223100/p/1241275.html
Copyright © 2020-2023  润新知