using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace Burgeon.Wing2.Common.Helper
{
public class FileHelper
{
/**/
///
/// 在根目录下创建文件夹
///
/// 要创建的文件路径
public void CreateFolder(string FolderPathName)
{
if (FolderPathName.Trim().Length > 0)
{
try
{
string CreatePath = System.Web.HttpContext.Current.Server.MapPath(FolderPathName).ToString();
//CreatePath = CreatePath;
if (!Directory.Exists(CreatePath))
{
Directory.CreateDirectory(CreatePath);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
/**/
///
/// 删除一个文件夹下面的字文件夹和文件
///
///
public void DeleteChildFolder(string FolderPathName)
{
if (FolderPathName.Trim().Length > 0)
{
try
{
string CreatePath = System.Web.HttpContext.Current.Server.MapPath(FolderPathName).ToString();
if (Directory.Exists(CreatePath))
{
Directory.Delete(CreatePath, true);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
/**/
///
/// 删除一个文件
///
///
public void DeleteFile(string FilePathName)
{
try
{
FileInfo DeleFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString());
DeleFile.Delete();
}
catch (Exception ex)
{
throw ex;
}
}
public void CreateFile(string FilePathName)
{
try
{
//创建文件夹
string[] strPath = FilePathName.Split('/');
CreateFolder(FilePathName.Replace("/" + strPath[strPath.Length - 1].ToString(), "")); //创建文件夹
FileInfo CreateFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString()); //创建文件
if (!CreateFile.Exists)
{
FileStream FS = CreateFile.Create();
FS.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
/**/
///
/// 删除整个文件夹及其字文件夹和文件
///
///
public void DeleParentFolder(string FolderPathName)
{
try
{
DirectoryInfo DelFolder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(FolderPathName).ToString());
if (DelFolder.Exists)
{
DelFolder.Delete();
}
}
catch(Exception ex)
{
throw ex;
}
}
/**/
///
/// 在文件里追加内容
///
///
public void ReWriteReadinnerText(string FilePathName, string WriteWord)
{
try
{
//建立文件夹和文件
//CreateFolder(FilePathName);
CreateFile(FilePathName);
//得到原来文件的内容
FileStream FileRead = new FileStream(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString(), FileMode.Open, FileAccess.ReadWrite);
StreamReader FileReadWord = new StreamReader(FileRead, System.Text.Encoding.Default);
string OldString = FileReadWord.ReadToEnd().ToString();
OldString = OldString + WriteWord;
//把新的内容重新写入
StreamWriter FileWrite = new StreamWriter(FileRead, System.Text.Encoding.Default);
FileWrite.Write(WriteWord);
//关闭
FileWrite.Close();
FileReadWord.Close();
FileRead.Close();
}
catch(Exception ce)
{
throw ce;
}
}
/**/
///
/// 在文件里追加内容
///
///
public string ReaderFileData(string FilePathName)
{
try
{
FileStream FileRead = new FileStream(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString(), FileMode.Open, FileAccess.Read);
StreamReader FileReadWord = new StreamReader(FileRead, System.Text.Encoding.Default);
string TxtString = FileReadWord.ReadToEnd().ToString();
//关闭
FileReadWord.Close();
FileRead.Close();
return TxtString;
}
catch(Exception ex)
{
throw ex;
}
}
/**/
///
/// 读取文件夹的文件
///
///
///
public DirectoryInfo checkValidSessionPath(string FilePathName)
{
try
{
DirectoryInfo MainDir = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName));
return MainDir;
}
catch(Exception ce)
{
throw ce;
}
}
/// <summary>
/// 利用 WinRAR 进行压缩 ,具体查看 http://www.okbase.net/doc/details/2582
/// </summary>
/// <param name="path">将要被压缩的文件夹(绝对路径)</param>
/// <param name="rarPath">压缩后的 .rar 的存放目录(绝对路径)</param>
/// <param name="rarName">压缩文件的名称(包括后缀)</param>
/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
public bool RAR(string path, string rarPath, string rarName)
{
bool flag = false;
string rarexe; //WinRAR.exe 的完整路径
//RegistryKey regkey; //注册表键
//Object regvalue; //键值
string cmd; //WinRAR 命令参数
ProcessStartInfo startinfo;
Process process;
try
{
rarexe = AppDomain.CurrentDomain.BaseDirectory + @"\WinRAR.exe";
string targetpath = System.IO.Path.GetDirectoryName(path);
if (!System.IO.Directory.Exists(targetpath))
System.IO.Directory.CreateDirectory(targetpath);
string targetrarPath = System.IO.Path.GetDirectoryName(rarPath);//存放路径不存在 创建
if (!System.IO.Directory.Exists(targetrarPath))
System.IO.Directory.CreateDirectory(targetrarPath);
//压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
cmd = string.Format("a {0} {1} -ep1 -o+ -inul -r -ibck",
rarName,
path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd; //设置命令参数
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口
startinfo.WorkingDirectory = rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit(); //无限期等待进程 winrar.exe 退出
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
/// <summary>
/// 利用 WinRAR 进行解压缩
/// </summary>
/// <param name="path">文件解压路径(绝对)</param>
/// <param name="rarPath">将要解压缩的 .rar 文件的存放目录(绝对路径)</param>
/// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)</param>
/// <returns>true 或 false。解压缩成功返回 true,反之,false。</returns>
public bool UnRAR(string path, string rarPath, string rarName)
{
bool flag = false;
string rarexe;
//RegistryKey regkey;
//Object regvalue;
string cmd;
ProcessStartInfo startinfo;
Process process;
try
{
rarexe = AppDomain.CurrentDomain.BaseDirectory + @"\WinRAR.exe";
string targetpath = System.IO.Path.GetDirectoryName(path);
if (!System.IO.Directory.Exists(targetpath))
System.IO.Directory.CreateDirectory(targetpath);
//解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压到当前文件夹
cmd = string.Format("x {0} {1} -y",
rarName,
path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.WorkingDirectory = rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
/// <summary>
/// 利用 WinRAR 进行压缩 ,具体查看 http://www.okbase.net/doc/details/2582
/// </summary>
/// <param name="path">将要被压缩的文件夹(绝对路径)</param>
/// <param name="rarPath">压缩后的 .rar 的存放目录(绝对路径)</param>
/// <param name="rarName">压缩文件的名称(包括后缀)</param>
/// <param name="xfileName">排除文件夹里所有文件,以及文件本身;多个文件直接以;隔开 如:bin ;obj</param>
/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
public bool RARXFM(string path, string rarPath, string rarName,string xfileName)
{
bool flag = false;
string rarexe; //WinRAR.exe 的完整路径
//RegistryKey regkey; //注册表键
//Object regvalue; //键值
string cmd; //WinRAR 命令参数
ProcessStartInfo startinfo;
Process process;
try
{
rarexe = AppDomain.CurrentDomain.BaseDirectory + @"\WinRAR.exe";
string targetpath = System.IO.Path.GetDirectoryName(path);
if (!System.IO.Directory.Exists(targetpath))
System.IO.Directory.CreateDirectory(targetpath);
string targetrarPath = System.IO.Path.GetDirectoryName(rarPath);//存放路径不存在 创建
if (!System.IO.Directory.Exists(targetrarPath))
System.IO.Directory.CreateDirectory(targetrarPath);
//压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
cmd = string.Format("a {0} {1} -ep1 -o+ -inul -r -ibck",
rarName,
path);
//-x*\\bin\\* -x*\\bin -x*\\obj\\* -x*\\obj ---排除bin,obj文件夹里所有文件,以及文件本身
#region 排除文件里所有文件已经文件本身
if (!string.IsNullOrEmpty(xfileName))
{
string[] arrarlist = xfileName.Split(';');
string arrtr = string.Empty;
foreach (string str in arrarlist)
{
arrtr += string.Format(" -x*\\{0}\\* -x*\\{0} ", str);
}
cmd = cmd + arrtr;
}
#endregion
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd; //设置命令参数
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口
startinfo.WorkingDirectory = rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit(); //无限期等待进程 winrar.exe 退出
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
}
}