asp.net文件操作类
/**
文件操作类
**/
#region 引用命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
#endregion
namespace CommonUtilities
{
/// <summary>
/// 文件操作类
/// </summary>
public class FileHelper
{
#region 检测指定目录是否存在
/// <summary>
/// 检测指定目录是否存在
/// </summary>
/// <param name="directoryPath">目录的绝对路径</param>
public static bool IsExistDirectory( string directoryPath )
{
return Directory.Exists( directoryPath );
}
#endregion
#region 检测指定文件是否存在
/// <summary>
/// 检测指定文件是否存在,如果存在则返回true。
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static bool IsExistFile( string filePath )
{
return File.Exists( filePath );
}
#endregion
#region 检测指定目录是否为空
/// <summary>
/// 检测指定目录是否为空
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static bool IsEmptyDirectory( string directoryPath )
{
try
{
//判断是否存在文件
string[] fileNames = GetFileNames( directoryPath );
if ( fileNames.Length > )
{
return false;
}
//判断是否存在文件夹
string[] directoryNames = GetDirectories( directoryPath );
if ( directoryNames.Length > )
{
return false;
}
return true;
}
catch ( Exception ex )
{
LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
return true;
}
}
#endregion
#region 检测指定目录中是否存在指定的文件
/// <summary>
/// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
public static bool Contains( string directoryPath, string searchPattern )
{
try
{
//获取指定的文件列表
string[] fileNames = GetFileNames( directoryPath, searchPattern, false );
//判断指定文件是否存在
if ( fileNames.Length == )
{
return false;
}
else
{
return true;
}
}
catch ( Exception ex )
{
LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
return false;
}
}
/// <summary>
/// 检测指定目录中是否存在指定的文件
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
/// <param name="isSearchChild">是否搜索子目录</param>
public static bool Contains( string directoryPath, string searchPattern, bool isSearchChild )
{
try
{
//获取指定的文件列表
string[] fileNames = GetFileNames( directoryPath, searchPattern, true );
//判断指定文件是否存在
if ( fileNames.Length == )
{
return false;
}
else
{
return true;
}
}
catch ( Exception ex )
{
LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
return false;
}
}
#endregion
#region 创建一个目录
/// <summary>
/// 创建一个目录
/// </summary>
/// <param name="directoryPath">目录的绝对路径</param>
public static void CreateDirectory( string directoryPath )
{
//如果目录不存在则创建该目录
if ( !IsExistDirectory( directoryPath ) )
{
Directory.CreateDirectory( directoryPath );
}
}
#endregion
#region 创建一个文件
/// <summary>
/// 创建一个文件。
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static void CreateFile( string filePath )
{
try
{
//如果文件不存在则创建该文件
if ( !IsExistFile( filePath ) )
{
//创建一个FileInfo对象
FileInfo file = new FileInfo( filePath );
//创建文件
FileStream fs = file.Create();
//关闭文件流
fs.Close();
}
}
catch ( Exception ex )
{
LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
throw ex;
}
}
/// <summary>
/// 创建一个文件,并将字节流写入文件。
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
/// <param name="buffer">二进制流数据</param>
public static void CreateFile( string filePath, byte[] buffer )
{
try
{
//如果文件不存在则创建该文件
if ( !IsExistFile( filePath ) )
{
//创建一个FileInfo对象
FileInfo file = new FileInfo( filePath );
//创建文件
FileStream fs = file.Create();
//写入二进制流
fs.Write( buffer, , buffer.Length );
//关闭文件流
fs.Close();
}
}
catch ( Exception ex )
{
LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
throw ex;
}
}
#endregion
#region 获取文本文件的行数
/// <summary>
/// 获取文本文件的行数
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static int GetLineCount( string filePath )
{
//将文本文件的各行读到一个字符串数组中
string[] rows = File.ReadAllLines( filePath );
//返回行数
return rows.Length;
}
#endregion
#region 获取一个文件的长度
/// <summary>
/// 获取一个文件的长度,单位为Byte
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static int GetFileSize( string filePath )
{
//创建一个文件对象
FileInfo fi = new FileInfo( filePath );
//获取文件的大小
return (int)fi.Length;
}
/// <summary>
/// 获取一个文件的长度,单位为KB
/// </summary>
/// <param name="filePath">文件的路径</param>
public static double GetFileSizeByKB( string filePath )
{
//创建一个文件对象
FileInfo fi = new FileInfo( filePath );
//获取文件的大小
return ConvertHelper.ToDouble( ConvertHelper.ToDouble( fi.Length ) / , );
}
/// <summary>
/// 获取一个文件的长度,单位为MB
/// </summary>
/// <param name="filePath">文件的路径</param>
public static double GetFileSizeByMB( string filePath )
{
//创建一个文件对象
FileInfo fi = new FileInfo( filePath );
//获取文件的大小
return ConvertHelper.ToDouble( ConvertHelper.ToDouble( fi.Length ) / / , );
}
#endregion
#region 获取指定目录中的文件列表
/// <summary>
/// 获取指定目录中所有文件列表
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static string[] GetFileNames( string directoryPath )
{
//如果目录不存在,则抛出异常
if ( !IsExistDirectory( directoryPath ) )
{
throw new FileNotFoundException();
}
//获取文件列表
return Directory.GetFiles( directoryPath );
}
/// <summary>
/// 获取指定目录及子目录中所有文件列表
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
/// <param name="isSearchChild">是否搜索子目录</param>
public static string[] GetFileNames( string directoryPath, string searchPattern, bool isSearchChild )
{
//如果目录不存在,则抛出异常
if ( !IsExistDirectory( directoryPath ) )
{
throw new FileNotFoundException();
}
try
{
if ( isSearchChild )
{
return Directory.GetFiles( directoryPath, searchPattern, SearchOption.AllDirectories );
}
else
{
return Directory.GetFiles( directoryPath, searchPattern, SearchOption.TopDirectoryOnly );
}
}
catch ( IOException ex )
{
throw ex;
}
}
#endregion
#region 获取指定目录中的子目录列表
/// <summary>
/// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static string[] GetDirectories( string directoryPath )
{
try
{
return Directory.GetDirectories( directoryPath );
}
catch ( IOException ex )
{
throw ex;
}
}
/// <summary>
/// 获取指定目录及子目录中所有子目录列表
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
/// <param name="isSearchChild">是否搜索子目录</param>
public static string[] GetDirectories( string directoryPath, string searchPattern, bool isSearchChild )
{
try
{
if ( isSearchChild )
{
return Directory.GetDirectories( directoryPath, searchPattern, SearchOption.AllDirectories );
}
else
{
return Directory.GetDirectories( directoryPath, searchPattern, SearchOption.TopDirectoryOnly );
}
}
catch ( IOException ex )
{
throw ex;
}
}
#endregion
#region 向文本文件写入内容
/// <summary>
/// 向文本文件中写入内容
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
/// <param name="content">写入的内容</param>
public static void WriteText( string filePath, string content )
{
//向文件写入内容
File.WriteAllText( filePath, content );
}
#endregion
#region 向文本文件的尾部追加内容
/// <summary>
/// 向文本文件的尾部追加内容
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
/// <param name="content">写入的内容</param>
public static void AppendText( string filePath, string content )
{
File.AppendAllText( filePath, content );
}
#endregion
#region 将现有文件的内容复制到新文件中
/// <summary>
/// 将源文件的内容复制到目标文件中
/// </summary>
/// <param name="sourceFilePath">源文件的绝对路径</param>
/// <param name="destFilePath">目标文件的绝对路径</param>
public static void Copy( string sourceFilePath, string destFilePath )
{
File.Copy( sourceFilePath, destFilePath, true );
}
#endregion
#region 将文件移动到指定目录
/// <summary>
/// 将文件移动到指定目录
/// </summary>
/// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
/// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
public static void Move( string sourceFilePath,string descDirectoryPath )
{
//获取源文件的名称
string sourceFileName = GetFileName( sourceFilePath );
if ( IsExistDirectory( descDirectoryPath ) )
{
//如果目标中存在同名文件,则删除
if ( IsExistFile( descDirectoryPath + "\\" + sourceFileName ) )
{
DeleteFile( descDirectoryPath + "\\" + sourceFileName );
}
//将文件移动到指定目录
File.Move( sourceFilePath, descDirectoryPath + "\\" + sourceFileName );
}
}
#endregion
#region 将流读取到缓冲区中
/// <summary>
/// 将流读取到缓冲区中
/// </summary>
/// <param name="stream">原始流</param>
public static byte[] StreamToBytes( Stream stream )
{
try
{
//创建缓冲区
byte[] buffer = new byte[stream.Length];
//读取流
stream.Read( buffer, , ConvertHelper.ToInt32( stream.Length ) );
//返回流
return buffer;
}
catch ( Exception ex )
{
LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
throw ex;
}
finally
{
//关闭流
stream.Close();
}
}
#endregion
#region 将文件读取到缓冲区中
/// <summary>
/// 将文件读取到缓冲区中
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static byte[] FileToBytes( string filePath )
{
//获取文件的大小
int fileSize = GetFileSize( filePath );
//创建一个临时缓冲区
byte[] buffer = new byte[fileSize];
//创建一个文件流
FileInfo fi = new FileInfo( filePath );
FileStream fs = fi.Open( FileMode.Open );
try
{
//将文件流读入缓冲区
fs.Read( buffer, , fileSize );
return buffer;
}
catch ( IOException ex )
{
LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
throw ex;
}
finally
{
//关闭文件流
fs.Close();
}
}
#endregion
#region 将文件读取到字符串中
/// <summary>
/// 将文件读取到字符串中
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static string FileToString( string filePath )
{
return FileToString( filePath, BaseInfo.DefaultEncoding );
}
/// <summary>
/// 将文件读取到字符串中
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
/// <param name="encoding">字符编码</param>
public static string FileToString( string filePath,Encoding encoding )
{
//创建流读取器
StreamReader reader = new StreamReader( filePath, encoding );
try
{
//读取流
return reader.ReadToEnd();
}
catch ( Exception ex )
{
LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );
throw ex;
}
finally
{
//关闭流读取器
reader.Close();
}
}
#endregion
#region 从文件的绝对路径中获取文件名( 包含扩展名 )
/// <summary>
/// 从文件的绝对路径中获取文件名( 包含扩展名 )
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static string GetFileName( string filePath )
{
//获取文件的名称
FileInfo fi = new FileInfo( filePath );
return fi.Name;
}
#endregion
#region 从文件的绝对路径中获取文件名( 不包含扩展名 )
/// <summary>
/// 从文件的绝对路径中获取文件名( 不包含扩展名 )
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static string GetFileNameNoExtension( string filePath )
{
//获取文件的名称
FileInfo fi = new FileInfo( filePath );
return fi.Name.Split( '.' )[];
}
#endregion
#region 从文件的绝对路径中获取扩展名
/// <summary>
/// 从文件的绝对路径中获取扩展名
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static string GetExtension( string filePath )
{
//获取文件的名称
FileInfo fi = new FileInfo( filePath );
return fi.Extension;
}
#endregion
#region 清空指定目录
/// <summary>
/// 清空指定目录下所有文件及子目录,但该目录依然保存.
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static void ClearDirectory( string directoryPath )
{
if ( IsExistDirectory( directoryPath ) )
{
//删除目录中所有的文件
string[] fileNames = GetFileNames( directoryPath );
for ( int i = ; i < fileNames.Length; i++ )
{
DeleteFile( fileNames[i] );
}
//删除目录中所有的子目录
string[] directoryNames = GetDirectories( directoryPath );
for ( int i = ; i < directoryNames.Length; i++ )
{
DeleteDirectory( directoryNames[i] );
}
}
}
#endregion
#region 清空文件内容
/// <summary>
/// 清空文件内容
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static void ClearFile( string filePath )
{
//删除文件
File.Delete( filePath );
//重新创建该文件
CreateFile( filePath );
}
#endregion
#region 删除指定文件
/// <summary>
/// 删除指定文件
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static void DeleteFile( string filePath )
{
if ( IsExistFile( filePath ) )
{
File.Delete( filePath );
}
}
#endregion
#region 删除指定目录
/// <summary>
/// 删除指定目录及其所有子目录
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static void DeleteDirectory( string directoryPath )
{
if ( IsExistDirectory( directoryPath ) )
{
Directory.Delete( directoryPath, true );
}
}
#endregion
}
}
asp.net文件操作类的更多相关文章
- ASP.NET 文件操作类
1.读取文件 2.写入文件 using System; using System.Collections.Generic; using System.IO; using System.Linq; us ...
- [C#] 常用工具类——文件操作类
/// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...
- 文件操作类CFile
CFile file; CString str1= L"写入文件成功!"; wchar_t *str2; if (!file.Open(L"Hello.txt" ...
- android 文件操作类简易总结
android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- java csv 文件 操作类
一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...
- Qt5:Qt文件操作类 QFile
在QT中,操作文件一般不使用C++提供的文件操作类 , 因为操作文件的时候,要用到C++提供的 string 类,而在QT中使用的是Qt自己实现的一个string类 QString .在Qt中使用C+ ...
- C# 文件操作类大全
C# 文件操作类大全 时间:2015-01-31 16:04:20 阅读:1724 评论:0 收藏:0 [点我收藏+] 标签: 1.创建文件夹 //usin ...
- Java文件操作类效率对比
前言 众所周知,Java中有多种针对文件的操作类,以面向字节流和字符流可分为两大类,这里以写入为例: 面向字节流的:FileOutputStream 和 BufferedOutputStream 面向 ...
随机推荐
- Lazy方式单列模式,一种线程安全模式的新选择
public class WeProxyClient { private static readonly Lazy<WeProxyClient> list = new ...
- Swift Strings and Characters
String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 ...
- 放弃使用jQuery实现动画
在Web开发的圈子里,开发人员经常觉得CSS动画是一种高性能web动画技术.假设想让网页载入的更快一些,就应该用纯CSS动画.事实上这样的观点是错误的,非常多开发人员早就放弃了javascript的动 ...
- Java获取客户端真实IP地址的两种方法
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- PHP调用WCF小结
新工作第三周,做了3年多的.Net,突然急转弯做PHP,漂移过弯,速度180迈 由于数据的整合,在项目中不得不使用PHP调用WCF 一头的雾水,网上相关的资料少又少,在phpChina发个帖子,还没有 ...
- EasyUI时间格式化
changeDateFormatNodate: function (cellval) { var date = new Date(parseInt(cellval.replace("/Dat ...
- Android使用webService
在android中使用webservice,首先要导入Android webservice支持包 ksoap2-android-assembly-3.3.0-jar-with-dependencies ...
- UITableView 隐藏多余的分割线
//隐藏多余的分割线 - (void)setExtraCellLineHidden: (UITableView *)tableView { UIView *view =[ [UIView alloc] ...
- Remove Duplicate Letters
316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...
- mysql的分页存储过程,能够传出总记录数
最近用mysql + asp.net来写网站,既然mysql已经支持存储过程了,那么像分页这么常用的东西,当然要用存储过程啦 不过在网上找了一些,发现都有一个特点——就是不能传出总记录数,干脆自己研究 ...