391 lines
14 KiB
C#
391 lines
14 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Data;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Win32;
|
|
|
|
namespace Zhaizj.Framework.Utils
|
|
{
|
|
/// <summary>
|
|
/// 文件操作类
|
|
/// </summary>
|
|
public class FileHelper
|
|
{
|
|
|
|
#region 取得文件后缀名
|
|
/****************************************
|
|
* 函数名称:GetPostfixStr
|
|
* 功能说明:取得文件后缀名
|
|
* 参 数:filename:文件名称
|
|
* 调用示列:
|
|
* string filename = "aaa.aspx";
|
|
* string s = EC.FileObj.GetPostfixStr(filename);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 取后缀名
|
|
/// </summary>
|
|
/// <param name="filename">文件名</param>
|
|
/// <returns>.gif|.html格式</returns>
|
|
public static string GetPostfixStr(string filename)
|
|
{
|
|
int start = filename.LastIndexOf(".");
|
|
int length = filename.Length;
|
|
string postfix = filename.Substring(start, length - start);
|
|
return postfix;
|
|
}
|
|
#endregion
|
|
|
|
#region 判断文件是否存在
|
|
/// <summary>
|
|
/// 判断文件是否存在
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static bool Exist(string path)
|
|
{
|
|
return System.IO.File.Exists(path);
|
|
}
|
|
#endregion
|
|
|
|
#region 获取指定目录下的文件
|
|
public static string[] GetFiles(string path)
|
|
{
|
|
return System.IO.Directory.GetFiles(path);
|
|
}
|
|
#endregion
|
|
|
|
#region 判断目录是否存在
|
|
/// <summary>
|
|
/// 判断目录是否存在
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static bool DirectoryExist(string path)
|
|
{
|
|
return System.IO.Directory.Exists(path);
|
|
}
|
|
#endregion
|
|
|
|
#region 写文件
|
|
/****************************************
|
|
* 函数名称:WriteFile
|
|
* 功能说明:写文件,会覆盖掉以前的内容
|
|
* 参 数:Path:文件路径,Strings:文本内容
|
|
* 调用示列:
|
|
* string Path = Server.MapPath("Default2.aspx");
|
|
* string Strings = "这是我写的内容啊";
|
|
* EC.FileObj.WriteFile(Path,Strings);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 写文件
|
|
/// </summary>
|
|
/// <param name="Path">文件路径</param>
|
|
/// <param name="Strings">文件内容</param>
|
|
public static void WriteFile(string Path, string Strings)
|
|
{
|
|
if (!System.IO.File.Exists(Path))
|
|
{
|
|
System.IO.FileStream f = System.IO.File.Create(Path);
|
|
f.Close();
|
|
}
|
|
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, false, System.Text.Encoding.Default);
|
|
f2.Write(Strings);
|
|
f2.Close();
|
|
f2.Dispose();
|
|
}
|
|
#endregion
|
|
|
|
#region 读文件
|
|
/****************************************
|
|
* 函数名称:ReadFile
|
|
* 功能说明:读取文本内容
|
|
* 参 数:Path:文件路径
|
|
* 调用示列:
|
|
* string Path = Server.MapPath("Default2.aspx");
|
|
* string s = EC.FileObj.ReadFile(Path);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 读文件
|
|
/// </summary>
|
|
/// <param name="Path">文件路径</param>
|
|
/// <returns></returns>
|
|
public static string ReadFile(string Path)
|
|
{
|
|
string s = "";
|
|
if (!System.IO.File.Exists(Path))
|
|
s = "不存在相应的目录";
|
|
else
|
|
{
|
|
StreamReader f2 = new StreamReader(Path, System.Text.Encoding.Default);
|
|
s = f2.ReadToEnd();
|
|
f2.Close();
|
|
f2.Dispose();
|
|
}
|
|
|
|
return s;
|
|
}
|
|
#endregion
|
|
|
|
#region 追加文件
|
|
/****************************************
|
|
* 函数名称:FileAdd
|
|
* 功能说明:追加文件内容
|
|
* 参 数:Path:文件路径,strings:内容
|
|
* 调用示列:
|
|
* string Path = Server.MapPath("Default2.aspx");
|
|
* string Strings = "新追加内容";
|
|
* EC.FileObj.FileAdd(Path, Strings);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 追加文件
|
|
/// </summary>
|
|
/// <param name="Path">文件路径</param>
|
|
/// <param name="strings">内容</param>
|
|
public static void FileAdd(string Path, string strings)
|
|
{
|
|
StreamWriter sw = System.IO.File.AppendText(Path);
|
|
sw.Write(strings);
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
#endregion
|
|
|
|
#region 拷贝文件
|
|
/****************************************
|
|
* 函数名称:FileCoppy
|
|
* 功能说明:拷贝文件
|
|
* 参 数:OrignFile:原始文件,NewFile:新文件路径
|
|
* 调用示列:
|
|
* string orignFile = Server.MapPath("Default2.aspx");
|
|
* string NewFile = Server.MapPath("Default3.aspx");
|
|
* EC.FileObj.FileCoppy(OrignFile, NewFile);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 拷贝文件
|
|
/// </summary>
|
|
/// <param name="OrignFile">原始文件</param>
|
|
/// <param name="NewFile">新文件路径</param>
|
|
public static void FileCoppy(string orignFile, string NewFile)
|
|
{
|
|
System.IO.File.Copy(orignFile, NewFile, true);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 删除文件
|
|
/****************************************
|
|
* 函数名称:FileDel
|
|
* 功能说明:删除文件
|
|
* 参 数:Path:文件路径
|
|
* 调用示列:
|
|
* string Path = Server.MapPath("Default3.aspx");
|
|
* EC.FileObj.FileDel(Path);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 删除文件
|
|
/// </summary>
|
|
/// <param name="Path">路径</param>
|
|
public static bool FileDel(string Path)
|
|
{
|
|
try
|
|
{
|
|
System.IO.File.Delete(Path);
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 移动文件
|
|
/****************************************
|
|
* 函数名称:FileMove
|
|
* 功能说明:移动文件
|
|
* 参 数:OrignFile:原始路径,NewFile:新文件路径
|
|
* 调用示列:
|
|
* string orignFile = Server.MapPath("../说明.txt");
|
|
* string NewFile = Server.MapPath("../../说明.txt");
|
|
* EC.FileObj.FileMove(OrignFile, NewFile);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 移动文件
|
|
/// </summary>
|
|
/// <param name="OrignFile">原始路径</param>
|
|
/// <param name="NewFile">新路径</param>
|
|
public static bool FileMove(string orignFile, string NewFile)
|
|
{
|
|
try
|
|
{
|
|
System.IO.File.Move(orignFile, NewFile);
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 在当前目录下创建目录
|
|
/****************************************
|
|
* 函数名称:FolderCreate
|
|
* 功能说明:在当前目录下创建目录
|
|
* 参 数:OrignFolder:当前目录,NewFloder:新目录
|
|
* 调用示列:
|
|
* string orignFolder = Server.MapPath("test/");
|
|
* string NewFloder = "new";
|
|
* EC.FileObj.FolderCreate(OrignFolder, NewFloder);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 在当前目录下创建目录
|
|
/// </summary>
|
|
/// <param name="OrignFolder">当前目录</param>
|
|
/// <param name="NewFloder">新目录</param>
|
|
public static void CreateFolder(string orignFolder, string NewFloder)
|
|
{
|
|
Directory.SetCurrentDirectory(orignFolder);
|
|
Directory.CreateDirectory(NewFloder);
|
|
}
|
|
/// <summary>
|
|
/// 自动创建文件夹
|
|
/// </summary>
|
|
/// <param name="folder"></param>
|
|
public static void CreateFolder(string folder)
|
|
{
|
|
Directory.CreateDirectory(folder);
|
|
}
|
|
#endregion
|
|
|
|
#region 递归删除文件夹目录及文件
|
|
/****************************************
|
|
* 函数名称:DeleteFolder
|
|
* 功能说明:递归删除文件夹目录及文件
|
|
* 参 数:dir:文件夹路径
|
|
* 调用示列:
|
|
* string dir = Server.MapPath("test/");
|
|
* EC.FileObj.DeleteFolder(dir);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 递归删除文件夹目录及文件
|
|
/// </summary>
|
|
/// <param name="dir"></param>
|
|
/// <returns></returns>
|
|
public static void DeleteFolder(string dir)
|
|
{
|
|
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
|
|
{
|
|
foreach (string d in Directory.GetFileSystemEntries(dir))
|
|
{
|
|
if (System.IO.File.Exists(d))
|
|
System.IO.File.Delete(d); //直接删除其中的文件
|
|
else
|
|
DeleteFolder(d); //递归删除子文件夹
|
|
}
|
|
Directory.Delete(dir); //删除已空文件夹
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
|
|
/****************************************
|
|
* 函数名称:CopyDir
|
|
* 功能说明:将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
|
|
* 参 数:srcPath:原始路径,aimPath:目标文件夹
|
|
* 调用示列:
|
|
* string srcPath = Server.MapPath("test/");
|
|
* string aimPath = Server.MapPath("test1/");
|
|
* EC.FileObj.CopyDir(srcPath,aimPath);
|
|
*****************************************/
|
|
/// <summary>
|
|
/// 指定文件夹下面的所有内容copy到目标文件夹下面
|
|
/// </summary>
|
|
/// <param name="srcPath">原始路径</param>
|
|
/// <param name="aimPath">目标文件夹</param>
|
|
public static void CopyDir(string srcPath, string aimPath)
|
|
{
|
|
try
|
|
{
|
|
// 检查目标目录是否以目录分割字符结束如果不是则添加之
|
|
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
|
|
aimPath += Path.DirectorySeparatorChar;
|
|
// 判断目标目录是否存在如果不存在则新建之
|
|
if (!Directory.Exists(aimPath))
|
|
Directory.CreateDirectory(aimPath);
|
|
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
|
|
//如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
|
|
//string[] fileList = Directory.GetFiles(srcPath);
|
|
string[] fileList = Directory.GetFileSystemEntries(srcPath);
|
|
//遍历所有的文件和目录
|
|
foreach (string file in fileList)
|
|
{
|
|
//先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
|
|
|
|
if (Directory.Exists(file))
|
|
CopyDir(file, aimPath + Path.GetFileName(file));
|
|
//否则直接Copy文件
|
|
else
|
|
System.IO.File.Copy(file, aimPath + Path.GetFileName(file), true);
|
|
}
|
|
}
|
|
catch (Exception ee)
|
|
{
|
|
throw new Exception(ee.ToString());
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region 获取文件的mime type
|
|
/// <summary>
|
|
/// 根据文件扩展名获取文件的 Mime-Type
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
public static string GetContentTypeForFileName(string fileName)
|
|
{
|
|
string mimeType = "application/unknown";
|
|
FileInfo fileinfo = new FileInfo(fileName);
|
|
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(
|
|
fileinfo.Extension.ToLower()
|
|
);
|
|
|
|
if (regKey != null)
|
|
{
|
|
object contentType = regKey.GetValue("Content Type");
|
|
|
|
if (contentType != null)
|
|
mimeType = contentType.ToString();
|
|
}
|
|
|
|
return mimeType;
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 获取随机的文件名
|
|
/// </summary>
|
|
/// <param name="length">随机名称长度,不包含前缀</param>
|
|
/// <param name="prefix">文件名前缀</param>
|
|
/// <returns></returns>
|
|
public static string GetRandomFileName(string prefix,int length)
|
|
{
|
|
return string.Format("{0}{1}", prefix, RandomHelper.GetRandomString(length, RandomHelper.READ_CHARLIST));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取随机的文件名
|
|
/// </summary>
|
|
/// <param name="length">随机名称长度</param>
|
|
/// <returns></returns>
|
|
public static string GetRandomFileName(int length)
|
|
{
|
|
return GetRandomFileName("", length);
|
|
}
|
|
}
|
|
} |