📄 filehelper.cs
字号:
//Copyright (C) 2006 dooogo.com
//author:benben
//www.aspxclub.com
using System;
using System.IO;
using System.Web.UI.HtmlControls;
namespace Club.Framework
{
/// <summary>
/// FileHelper 的摘要说明。
/// </summary>
public class FileHelper
{
private int fileSize=0;
/// <summary>
/// 文件尺寸 单位k
/// </summary>
public int FileSize
{
get
{
return fileSize;
}
set
{
fileSize=value;
}
}
private string dirPath;
/// <summary>
/// 上传路径目录
/// </summary>
public string DirPath
{
get
{
return dirPath;
}
set
{
dirPath=value;
}
}
private string fileType;
/// <summary>
/// 文件类型,如gif,jpg,bmp,空字符为不限制上传类型
/// </summary>
public string FileType
{
get
{
return fileType;
}
set
{
fileType=value;
}
}
/// <summary>
/// 初始化文件操作类
/// </summary>
public FileHelper()
{
}
public FileHelper(string dirPath)
{
this.dirPath = dirPath;
}
/// <summary>
/// 上传文件,文件名不改变
/// </summary>
/// <param name="fl_Name">上传文件控件</param>
/// <returns>文件名</returns>
public string UpFile(HtmlInputFile fl_Name)
{
if(fileType!=null)
{
if(StringHelper.GetStrCount(this.FileType,StringHelper.GetLastStr(fl_Name.PostedFile.FileName,".").ToLower())==0&&this.FileType!=string.Empty)
{
throw new System.Exception("请上传"+this.FileType+"文件格式文件");
}
}
if(this.fileSize!=0)
{
if(fl_Name.PostedFile.ContentLength/1024>this.FileSize)
{
throw new System.Exception("文件大小限定在"+this.FileSize+"KB以内");
}
}
if(File.Exists(Globals.GetFilePath(this.DirPath+Path.GetFileName(fl_Name.PostedFile.FileName))))
{
throw new System.Exception("该文件已经存在,请改名再上传");
}
string strFullPath=Globals.GetFilePath(this.DirPath+Path.GetFileName(fl_Name.PostedFile.FileName));
fl_Name.PostedFile.SaveAs(strFullPath);
return Path.GetFileName(fl_Name.PostedFile.FileName);
}
/// <summary>
/// 上传文件,文件名为随机数
/// </summary>
/// <param name="fl_Name">文件上传控件</param>
/// <returns>已上传文件名</returns>
public string UpFileRandom(HtmlInputFile fl_Name)
{
if(fileType!=null)
{
if(StringHelper.GetStrCount(this.FileType,StringHelper.GetLastStr(fl_Name.PostedFile.FileName,".").ToLower())==0&&this.FileType!=string.Empty)
{
throw new System.Exception("请上传"+this.FileType+"文件格式文件");
}
}
string filename;
if(this.fileSize!=0)
{
if(fl_Name.PostedFile.ContentLength/1024>this.FileSize)
{
throw new System.Exception("文件大小限定在"+this.FileSize+"KB以内");
}
}
string strPath=Globals.GetFilePath(this.DirPath);
filename=StringHelper.GetDataRandom();
filename=filename+"."+StringHelper.GetLastStr(fl_Name.PostedFile.FileName,".");
string strFullPath=strPath+filename;
if(File.Exists(strFullPath))
{
throw new System.Exception("该文件已经存在,请改名再上传");
}
fl_Name.PostedFile.SaveAs(strFullPath);
return filename;
}
/// <summary>
/// 上传图片格式文档,不改文件名
/// </summary>
/// <param name="fl_Name">文件上传控件</param>
/// <returns>已上传文件名</returns>
public string UpImage(HtmlInputFile fl_Name)
{
this.FileType = "gif,jpeg,png,jpg,bmp";
return this.UpFile(fl_Name);
}
/// <summary>
/// 上传图片格式文档,文件名为随机数
/// </summary>
/// <param name="fl_Name">文件上传控件</param>
/// <returns>已上传文件名</returns>
public string UpImageRandom(HtmlInputFile fl_Name)
{
this.FileType = "gif,jpeg,png,jpg,bmp";
return this.UpFileRandom(fl_Name);
}
/// <summary>
/// 强制删除一个存在文件的文件夹
/// </summary>
/// <param name="strDir">文件夹相对路径,比如:~/downloads/</param>
public void DelFolder(string strDir)
{
strDir = Globals.GetFilePath(strDir);
if (Directory.Exists(strDir))
{
string[] arrayFolder=System.IO.Directory.GetFileSystemEntries(strDir);
for (int i=0;i<arrayFolder.Length;i++)
{
if (StringHelper.GetStrCount(arrayFolder[i],".")>0)
{
System.IO.File.Delete(arrayFolder[i]);
}
else
{
DelFolder(arrayFolder[i]);
}
}
System.IO.Directory.Delete(strDir);
}
}
/// <summary>
/// 删除文件夹
/// </summary>
/// <param name="strDir">相对目录</param>
/// <param name="isDelSub">存在子文件夹是否删除</param>
public static void DelFolder(string strDir,bool isDelSub)
{
strDir = Globals.GetFilePath(strDir);
if (Directory.Exists(strDir))
{
System.IO.Directory.Delete(strDir,true);
}
}
/// <summary>
/// 增加一个文件夹-如果存在将停止,并返回是否已创建
/// </summary>
/// <param name="strDir">文件夹相对路径,比如:~/downloads/</param>
public static bool AddFolder(string strDir)
{
strDir = Globals.GetFilePath(strDir);
if(!Directory.Exists(strDir))
{
System.IO.Directory.CreateDirectory(strDir);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 强制创建文件夹--如果存在将覆盖一个存在文件的文件夹
/// </summary>
/// <param name="strDir">文件夹相对路径,比如:~/downloads/wufeng/</param>
public void AddFolderForce(string strDir)
{
strDir = Globals.GetFilePath(strDir);
if(Directory.Exists(strDir))
{
string[] arrayFolder=System.IO.Directory.GetFileSystemEntries(strDir);
for (int i=0;i<arrayFolder.Length;i++)
{
if (StringHelper.GetStrCount(arrayFolder[i],".")>0)
{
System.IO.File.Delete(arrayFolder[i]);
}
else
{
DelFolder(arrayFolder[i]);
}
}
System.IO.Directory.Delete(strDir);
}
System.IO.Directory.CreateDirectory(strDir);
}
/// <summary>
/// 删除一个文件--返回删除信息
/// </summary>
/// <param name="strPath">文件夹相对路径,比如:~/downloads/wufeng/aaa.aspx</param>
public bool DelFile(string strPath)
{
strPath = Globals.GetFilePath(strPath);
if(File.Exists(strPath))
{
System.IO.File.Delete(strPath);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 删除一个文件--返回删除信息
/// </summary>
/// <param name="strPath">文件夹相对路径,比如:~/downloads/wufeng/aaa.aspx</param>
/// <returns></returns>
public static bool DelFileExe(string strPath)
{
strPath = Globals.GetFilePath(strPath);
if(File.Exists(strPath))
{
System.IO.File.Delete(strPath);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 以二进制读取文件-返回文本
/// </summary>
/// <param name="strPath">文件夹相对路径,比如:downloads/wufeng/aaa.aspx</param>
/// <param name="strCode">读取编码,如GB2312,utf-8</param>
public static string GetFileText(string strPath,string strCode)
{
string file_content=string.Empty;
strPath =Globals.GetFilePath(strPath);
if (File.Exists(strPath))
{
Stream s = File.OpenRead(strPath);
StreamReader r = new StreamReader(s,System.Text.Encoding.GetEncoding(strCode));
file_content=r.ReadToEnd();
r.Close();
s.Close();
}
else
{
throw new System.Exception("要读取的文件不存在!");
}
return file_content;
}
/// <summary>
/// 以二进制写入文件
/// </summary>
/// <param name="strPath">文件夹相对路径,比如:~/downloads/wufeng/aaa.aspx</param>
/// <param name="content">要写入的文本</param>
/// <param name="isAdd">是否追加文本,false为重新创建</param>
/// <param name="strCode">写入编码,如GB2312,utf-8</param>
public static void SaveFileText(string strPath,string content,bool isAdd,string strCode)
{
strPath = Globals.GetFilePath(strPath);
if(!File.Exists(strPath))
{
File.Create(strPath);
}
StreamWriter w = new StreamWriter(strPath,isAdd, System.Text.Encoding.GetEncoding(strCode));
w.WriteLine(content);
w.Close();
}
/// <summary>
/// 检查文件是否存在
/// </summary>
/// <param name="filePath">相对路径 如~/images/aaa.jpg</param>
/// <returns></returns>
public static bool CheckFileExists(string filePath)
{
return System.IO.File.Exists(Globals.GetFilePath(filePath));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -