C# 文件帮助类
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 = ;
// 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 / ) > )//表示可以转换成KB
{
m = m / ;//转换成KB if ((m / ) > )//表示可以转换成MB
{
m = m / ;//转换成MB了 if ((m / ) > )//表示可以转换成GB
{
m = m / ;//转换成GB了
viewstr = m.ToString() + "GB";
}
else
{
viewstr = m.ToString() + "MB";
}
}
else
{
viewstr = m.ToString() + "KB";
}
}
else
{
viewstr = m.ToString() + "byte";
}
return viewstr;
}
/// <summary>
/// 删除指定目录和内容
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
public static bool delDir(string dir)
{
bool flag = false;
DirectoryInfo d = new DirectoryInfo(dir);
if (d.Exists)//判断目录是否存在
{
try
{
d.Delete();
flag = true;
}
catch (Exception e) { flag = false; }
}
return flag;
}
/// <summary>
/// 删除指定文件
/// </summary>
/// <param name="fil"></param>
/// <returns></returns>
public static bool delFile(string fil)
{
bool flag = false;
FileInfo d = new FileInfo(fil);
if (d.Exists)//判断目录是否存在
{
try
{
d.Delete();
flag = true;
}
catch (Exception e) { flag = false; }
}
return flag;
}
public static void Copy(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); CopyAll(diSource, diTarget);
}
/// <summary>
/// 复制目录及子文件到指定目录
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
} // Copy each file into it's new directory.
foreach (FileInfo fi in source.GetFiles())
{
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
} // Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
} /// <summary>
/// 循环读取某个目录下的所有文件和目录,查询有关每一项的一些信息。返回一个文件列表
/// </summary>
/// <param name="strCurrentDir"></param>
public static List<fileEntity> FileView(string strCurrentDir)
{
List<fileEntity> fileList = new List<fileEntity>();
DirectoryInfo dir = new DirectoryInfo(strCurrentDir); foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())//这个循环再读取文件的信息
{
try
{
//FileSystemInfo 对象可以表示文件或目录,从而可以作为 FileInfo 或 DirectoryInfo 对象的基础。 当分析许多文件和目录时,请使用该基类。
FileInfo fi;
DirectoryInfo di;
//创建一个自己写的实体类的实体
fileEntity newfile = new fileEntity();
if (fsi is FileInfo)//外层循环读取文件信息
{
//表示当前fsi是文件
fi = (FileInfo)fsi;
newfile.fileName = fi.Name;
newfile.fileExt = fi.Extension;
newfile.fileSize = fi.Length;
newfile.FileModify = fi.LastWriteTime;
//通过扩展名来选择文件显示图标
switch (newfile.fileExt)
{
case ".gif":
newfile.picName = "gif.gif";
break;
default:
newfile.picName = "other.gif";
break;
}
newfile.picName = "<img src='" + newfile.picName + "' width=25 height=20>";
}
else
{
di = (DirectoryInfo)fsi;
newfile.fileName = di.Name;
newfile.fileSize = DirSize(di);//调用计算文件夹大小的方法
newfile.FileModify = di.LastWriteTime;
newfile.picName = "<img src='directory.gif' width=25 height=20>";
}
fileList.Add(newfile);
}
catch (Exception e) { }
}
return fileList; } /// <summary>
/// 显示目录和文件
/// </summary>
/// <param name="path"></param>
public static void All(string path)
{
FileInfo fi;//文件
DirectoryInfo di;//目录
DirectoryInfo dir=null;
int i = ; //控制行的颜色
try
{
dir = new DirectoryInfo(path);
}
catch (Exception e) { }
foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
{
try
{
fileEntity newfile = new fileEntity();
FolderEntity folder = new FolderEntity();
newfile.fileName = "";
newfile.picName = "";
newfile.fileExt = "";
newfile.fileSize = ;
folder.folderName = "";
folder.picName = ""; i += ;
if (fsi is FileInfo)//判断当前读取的是不是一个文件
{
//表示当前fsi是文件
fi = (FileInfo)fsi;
newfile.fileName = fi.Name;
newfile.fileExt = fi.Extension;
newfile.fileSize = fi.Length;
newfile.FileModify = fi.LastWriteTime; //将文件加上可以下载文件的链接 newfile.fileName = "<a href='........'></a>"; //通过扩展名来选择文件显示图标 //Response.Write(Session["DataBasePath"].ToString()+"\\filetype\\"+pfun.getFileExt(FileExt)+".gif"); if (fsi.Exists)
{
switch (newfile.fileExt)
{
case ".gif":
newfile.picName = "gif.gif";
break;
default:
newfile.picName = "other.gif";
break;
}
}
else
{
newfile.picName = "unknown.gif";
} /*
switch(FileExt)
{
case ".gif":
FilePic = "gif.gif";
break;
default:
FilePic = "other.gif";
break;
}
*/ newfile.picName = "<img src='filetype/" + newfile.picName + "' width=16 height=16>"; }
else
{
//当前为目录
di = (DirectoryInfo)fsi;
folder.folderName = di.Name; //给目录加上链接 folder.folderName = "<a href='.......'><a>";
folder.lastTime = di.LastWriteTime;
folder.picName = "<img src='filetype/folder.gif' width=16 height=16>"; }
}catch(Exception e){}
} }
}
}
C# 文件帮助类的更多相关文章
- php 文件日志类
php文件日志类,按年月日组织目录结构. <?php class FileLog { private $_filepath; //文件路径 private $_filename; //日志文件名 ...
- C++文件流类与文件流对象
文件流是以外存文件为输入输出对象的数据流.输出文件流是从内存流向外存文件的数据,输入文件流是从外存文件流向内存的数据.每一个文件流都有一个内存缓冲区与之对应. 请区分文件流与文件的概念,不用误以为文件 ...
- 使用Java的多线程和IO流写一个文件复制功能类
创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...
- Code片段 : .properties属性文件操作工具类 & JSON工具类
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...
- PHP文件处理类
/** * 文件读写类 * 读取时,支持跳过N个/行字符然后再读取M个/行字符 * 支持每次读取时使用回调函数 * * 示例: * $file = new File('a.txt', 'r'); * ...
- 自动扫描FTP文件工具类 ScanFtp.java
package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- 读取Config文件工具类 PropertiesConfig.java
package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...
- C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭
迄今为止,我们讨论的输入输出是以系统指定的标准设备(输入设备为键盘,输出设备为显示器)为对象的.在实际应用中,常以磁盘文件作为对象.即从磁盘文件读取数据,将数据输出到磁盘文件.磁盘是计算机的外部存储器 ...
- C# - 文件操作类
除了封装数据流的类 System.IO命名空间中还提供了可以操作文件和目录的类 Directory类 ns:System.IO Level:Object=>Directory 表示目录的类 用于 ...
- php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event
php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event
随机推荐
- Type Correlation
Types of correlation: Logical correlation: Using pre-defined and customized correlation rules. Inven ...
- OpenRisc-48-or1200的SPRS模块分析
引言 之前,我们在分析or1200的WB模块时(http://blog.csdn.net/rill_zhen/article/details/10220619),介绍了OpenRISC的GPRS(ge ...
- iOS 倒计时
// // RootViewController.m // MyTimerDemo // // Created by huluo on 1/21/14. // Copyright (c) 2014 b ...
- 谷歌page speed 安装使用及页面问题详解
原文地址:http://wenku.baidu.com/view/b0a61f3ebcd126fff7050b40.html 谷歌page speed 安装使用及页面问题详解 谷歌page speed ...
- MyGui笔记(1)建立第一个工程
记录下学习 MyGui的一些笔记,从建立第一个工程开始. 步骤: 1.右键MYGUI解决方案,添加→新建项目,选择“Win32 项目”,名称为:TestHello.下一步,勾选“空项目”. 2.设置工 ...
- setObject与setValue的区别
在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有.1.先看看setVa ...
- 连接远程hbase长时间等待问题
确保本地保存了远程主机名: 保存远程hosts
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server. (文档 ID 885522.1)
APPLIES TO: PL/SQL - Version 9.2.0.1 to 12.1.0.1 [Release 9.2 to 12.1]Information in this document a ...
- zen-Coding的使用
zen-Coding的使用 zen-Coding的使用需要掌握CSS和HTML相关知识.其实只要对CSS的选择器比较熟悉,就可以得用简短的类似于CSS选择器的代码高效的编写出HTML代码.打开Note ...
- hdu4507
数位dp,终于守得云开见月明了.建议初学者先试试两道比较简单的hdu2089,hdu3555. 鸣谢:http://blog.csdn.net/acm_cxlove/article/details/8 ...