274 lines
7.5 KiB
C#
274 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace AllBase
|
|
{
|
|
public static class FileBase
|
|
{
|
|
/// <summary>
|
|
/// 文件是否存在
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static bool Exists(string path)
|
|
{
|
|
try
|
|
{
|
|
return File.Exists(path);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 读取文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static string ReadAllText(string path)
|
|
{
|
|
return ReadAllText(path, Encoding.Default);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="encoding"></param>
|
|
/// <returns></returns>
|
|
public static string ReadAllText(string path, Encoding encoding)
|
|
{
|
|
try
|
|
{
|
|
return File.ReadAllText(path, encoding);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static string[] ReadAllLines(string path, Encoding encoding)
|
|
{
|
|
try
|
|
{
|
|
return File.ReadAllLines(path, encoding);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static string[] ReadAllLines(string path)
|
|
{
|
|
return ReadAllLines(path, Encoding.Default);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 写入文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="content"></param>
|
|
/// <param name="encoding"></param>
|
|
/// <returns></returns>
|
|
public static bool WriteAllText(string path, string content, Encoding encoding)
|
|
{
|
|
try
|
|
{
|
|
File.WriteAllText(path, content, encoding);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static byte[] ReadBytes(string path)
|
|
{
|
|
try
|
|
{
|
|
return File.ReadAllBytes(path);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static bool WriteAllText(string path, string content)
|
|
{
|
|
return WriteAllText(path, content, Encoding.Default);
|
|
}
|
|
public static bool WriteBytes(string path, byte[] content)
|
|
{
|
|
try
|
|
{
|
|
File.WriteAllBytes(path, content);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 写入文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="content"></param>
|
|
/// <param name="encoding"></param>
|
|
/// <returns></returns>
|
|
public static bool AppendAllText(string path, string content, Encoding encoding)
|
|
{
|
|
try
|
|
{
|
|
File.AppendAllText(path, content, encoding);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool AppendAllText(string path, string content)
|
|
{
|
|
return AppendAllText(path, content, Encoding.Default);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static bool Delete(string path)
|
|
{
|
|
try
|
|
{
|
|
File.Delete(path);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 创建目录
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static bool DirectoryCreate(string path)
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool Move(string oldFile, string newFile)
|
|
{
|
|
try
|
|
{
|
|
Delete(newFile);
|
|
File.Move(oldFile, newFile);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogSave.Add(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将CSV文件的一行转换为列表,进行逗号分隔(字段内容中的逗号不参与分隔)
|
|
/// </summary>
|
|
/// <param name="strLine"></param>
|
|
/// <returns></returns>
|
|
public static List<string> CsvStrToList(string csvLine)
|
|
{
|
|
var newStr = string.Empty;
|
|
List<string> sList = new List<string>();
|
|
|
|
bool isSplice = false;
|
|
string[] array = csvLine.Split(new char[] { ',' });
|
|
foreach (var str in array)
|
|
{
|
|
var strTemp = str.Trim();
|
|
if (!string.IsNullOrEmpty(strTemp) && strTemp.IndexOf('"') > -1)
|
|
{
|
|
if (strTemp.StartsWith("\"") && !strTemp.StartsWith("\"\"") && !strTemp.EndsWith("\""))
|
|
{
|
|
isSplice = true;
|
|
}
|
|
if (strTemp.EndsWith("\"") && !strTemp.EndsWith("\"\""))
|
|
{
|
|
if (!isSplice)
|
|
newStr += strTemp;
|
|
else
|
|
newStr = newStr + "," + strTemp;
|
|
|
|
isSplice = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrEmpty(newStr))
|
|
newStr += strTemp;
|
|
}
|
|
|
|
if (isSplice)
|
|
{
|
|
//添加因拆分时丢失的逗号
|
|
if (string.IsNullOrEmpty(newStr))
|
|
newStr += strTemp;
|
|
else
|
|
newStr = newStr + "," + strTemp;
|
|
}
|
|
else
|
|
{
|
|
sList.Add(newStr.Replace("\"\"", "#@%*^^*%@#").Replace("\"", "").Trim(' ', '\t').Replace("#@%*^^*%@#", "\"")); // 去除字符中的双引号和首尾空格
|
|
newStr = string.Empty;
|
|
}
|
|
}
|
|
return sList;
|
|
}
|
|
|
|
}
|
|
}
|