日期:2014-05-17 浏览次数:21043 次
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Security.AccessControl;
using System.Security.Permissions;
namespace Bll
{
public class FolderHelper
{
//判断文件夹是否存在
public static bool checkFolderExits(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)//文件夹存在
{
return true;
}
else
{
//dir.Create();//不存在就创建一个
return false;
}
}
//创建一个文件夹,存在就创建失败
public static bool CreateNewFolder(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
if (!dir.Exists)
{
dir.Create();
return true;
}
else
return false;
}
/// <summary>
/// 在指定目录下创建指定名称文件夹
/// </summary>
/// <param name="ParentsPath"></param>
/// <param name="NewFolderName"></param>
/// <returns></returns>
public static bool CreateNewFolder(string ParentsPath, string NewFolderName)
{
string CreatePath = ParentsPath + @"\" + NewFolderName;
DirectoryInfo dir = new DirectoryInfo(CreatePath);
if (!dir.Exists)
{
dir.Create();
return true;
}
else
return false;
}
/// <summary>
/// 返回目录下的所有文件名
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static ArrayList getAllFiles(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
FileInfo[] fileinfo = dir.GetFiles();
ArrayList list = new ArrayList();
foreach (FileInfo f in fileinfo)
{
list.Add(f.Name);
}
return list;
}
else
return null;
}
/// <summary>
/// 计算文件夹的大小
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public static long DirSize(DirectoryInfo d)
{
long Size = 0;
// Add file sizes.
FileInfo[] fis = d.GetFiles();//获得目录文件列表
foreach (FileInfo fi in fis)
{
Size += fi.Length;
}
// Add subdirectory sizes.
DirectoryInfo[] dis = d.GetDirectories();//获取目录子目录列表
foreach (DirectoryInfo di in dis)
{
Size += DirSize(di);
}
return Size;
}
/// <summary>
/// 把文件夹得大小转换成比较合适的表示单位
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public static string ViewSize(long size)
{
long m=size;
string viewstr;
if ((m / 1024) > 0)//表示可以转换成KB
{
m = m / 1024;//转换成KB
if ((m / 1024) > 0)//表示可以转换成MB
{
m = m / 1024;//转换成MB了
if ((m / 1024) > 0)//表示可以转换成GB
{
m = m / 1024;//转换成GB了
viewstr = m.ToString() + "GB";
}
else
{
viewstr = m.ToString() + "MB";