You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
9.4 KiB

/*******************************************************/
/*Project:
Module :
Description :
Date : 2008-6-3 15:09:12
Create : Lxc
Update : 2014-12-31
TODO : */
/*******************************************************/
namespace EC.Util.Common;
/// <summary>
/// 文件操作单元
/// </summary>
public partial class FileUnit
{
private static readonly string AppFilepatch = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
/// <summary>
/// 创建目录
/// </summary>
/// <param name="fullPath"></param>
/// <returns></returns>
public static bool CreateDir(string fullPath)
{
if (!Directory.Exists(fullPath))
{
try
{
Directory.CreateDirectory(fullPath);
return true;
}
catch (Exception ex)
{
return false;
}
}
return true;
}
//删除文件名 获取路径
public static string GetDirectory(string fullPath)
{
return Path.GetDirectoryName(fullPath);
//return allFileName.Substring(0, allFileName.LastIndexOf('\\'));
}
/// <summary>
/// 得到文件名称
/// </summary>
/// <param name="fullPath"></param>
/// <returns></returns>
public static string GetFileName(string fullPath)
{
return Path.GetFileName(fullPath);
}
/// <summary>
/// 得到扩展名
/// </summary>
/// <param name="fullPath"></param>
/// <returns></returns>
public static string GetFileExt(string fullPath)
{
return Path.GetExtension(fullPath);
}
/// <summary>
/// 得到名称没有扩展名
/// </summary>
/// <param name="fullPath"></param>
/// <returns></returns>
public static string GetFileNameNoExt(string fullPath)
{
return Path.GetFileNameWithoutExtension(fullPath);
}
/// <summary>
/// 复制文件
/// </summary>
/// <param name="sourceFileName"></param>
/// <param name="destFileName"></param>
/// <returns></returns>
public static string CopyFile(string sourceFileName, string destFileName)
{
try
{
if (File.Exists(sourceFileName))
{
string destDirectory = GetDirectory(destFileName);// destFileName.Substring(0, destFileName.LastIndexOf('\\'));//删除文件名 获取路径
bool createDir = CreateDir(destDirectory);
if (createDir)
{
File.Copy(sourceFileName, destFileName);
}
else
{
return "文件路径" + destFileName + "不正确!";
}
}
else
{
return "文件路径" + sourceFileName + "不存在!";
}
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 得到路径 没有扩展名
/// </summary>
/// <param name="rptPath"></param>
/// <returns></returns>
public static string GetPathNoExt(string rptPath)
{
string rtpDir = GetDirectory(rptPath);
string disFileName = GetFileNameNoExt(rptPath);
string disPatch = rtpDir + "\\" + disFileName;// +".docx";
return disPatch;
}
/// <summary>
/// 读取文本
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static string ReadtxtFile(string filename)
{
try
{
if (File.Exists(filename))
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filename, System.Text.Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
else LogUnit.Error(typeof(FileUnit), "no exists " + filename);
}
catch (Exception e)
{
LogUnit.Error(typeof(FileUnit), e.ToString());
}
return "";
}
/// <summary>
/// 读取多行文本
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static List<string> ReadtxtLinesFile(string filename)
{
List<string> filtxtLines = new List<string>();
try
{
if (File.Exists(filename))
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filename, System.Text.Encoding.UTF8))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
filtxtLines.Add(line);
}
}
}
}
catch (Exception e)
{
LogUnit.Error(typeof(FileUnit), e.ToString());
}
return filtxtLines;
}
/// <summary>
/// 读取多行文本
/// </summary>
/// <param name="filepatch"></param>
/// <param name="filename"></param>
/// <param name="txtLines"></param>
/// <returns></returns>
public static bool WritetxtFileLine(string filepatch, string filename, List<string> txtLines)
{
List<string> filtxtLines = new List<string>();
try
{
if (!Directory.Exists(filepatch))
{
Directory.CreateDirectory(filepatch);
}
using (StreamWriter sw = new StreamWriter(filepatch + filename, false, System.Text.Encoding.UTF8))
{
foreach (string str in txtLines)
{
sw.WriteLine(str);
}
sw.Close();
}
}
catch (Exception e)
{
LogUnit.Error(typeof(FileUnit), e.ToString());
return false;
}
return true;
}
/// <summary>
/// 写入文本
/// </summary>
/// <param name="filepatch"></param>
/// <param name="filename"></param>
/// <param name="text"></param>
/// <returns></returns>
public static bool WritetxtFile(string filepatch, string filename, string text)
{
List<string> filtxtLines = new List<string>();
try
{
if (!Directory.Exists(filepatch))
{
Directory.CreateDirectory(filepatch);
}
using (StreamWriter sw = new StreamWriter(filepatch + filename, false, System.Text.Encoding.UTF8))
{
sw.Write(text);
sw.Close();
}
}
catch (Exception e)
{
LogUnit.Error(typeof(FileUnit), e.ToString());
return false;
}
return true;
}
private static bool Log(string subfilepatch, string logmessage)
{
try
{
string logfilePath = AppFilepatch + "Log\\" + subfilepatch;
if (!Directory.Exists(logfilePath))
{
Directory.CreateDirectory(logfilePath);
}
using (StreamWriter sw = new StreamWriter(logfilePath + DateTime.Now.ToString("yyyyMMdd") + ".txt", true, System.Text.Encoding.UTF8))
{
sw.WriteLine(DateTime.Now.ToString() + " : " + logmessage);
sw.Close();
}
}
catch (Exception e)
{
return false;
}
return true;
}
//支付日志
public static void PayLog(string log)
{
string path = "PayLog\\";
Log(path, log);
}
//订单日志
public static void OrderLog(string log)
{
string path = "OrderLog\\";
Log(path, log);
}
//写入日志
public static void Log(string log)
{
string path = "";
Log(path, log);
}
public static void WebLog(string log)
{
Log(log);
}
//public static void WebLog(string log)
//{
// string path = HttpContext.Current.Server.MapPath(".")+"Web\\";
// Log(path, log);
//}
public static void ErrLog(string log)
{
string subpath = "ErrLog\\";
Log(subpath, log);
}
public static void HisLog(string log)
{
string subpath = "HisLog\\";
Log(subpath, log);
}
public static void Package(string log)
{
string path = "Package\\";
Log(path, log);
}
public static void EPS(string log)
{
string path = "EPS\\";
Log(path, log);
}
public static void AllLog(string log)
{
string path = "AllLog\\";
Log(path, log);
}
public static bool Exists(string path)
{
return File.Exists(path);
}
public static void DeleteFile(string path)
{
try
{
if (File.Exists(path))
{
File.Delete(path);
}
else
{
LogUnit.Error(typeof(FileUnit), "路径不存在" + path);
}
}
catch (Exception ex)
{
LogUnit.Error(typeof(FileUnit), ex.ToString());
}
}
}