一个文件夹操作类
//杨春
//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();
}
}
}
}
本文来自博客园,作者:古道轻风,转载请注明原文链接:https://www.cnblogs.com/88223100/archive/2008/07/12/1241275.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析