using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace AllBase
{
public static class FileBase
{
///
/// 文件是否存在
///
///
///
public static bool Exists(string path)
{
try
{
return File.Exists(path);
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
///
/// 读取文件
///
///
///
public static string ReadAllText(string path)
{
return ReadAllText(path, Encoding.Default);
}
///
/// 读取文件
///
///
///
///
public static string ReadAllText(string path, Encoding encoding)
{
try
{
return File.ReadAllText(path, encoding);
}
catch (Exception e)
{
LogSave.Add(e.Message);
return null;
}
}
///
/// 读取文件
///
///
///
public static string[] ReadAllLines(string path, Encoding encoding)
{
try
{
return File.ReadAllLines(path, encoding);
}
catch (Exception e)
{
LogSave.Add(e.Message);
return null;
}
}
///
/// 读取文件
///
///
///
public static string[] ReadAllLines(string path)
{
return ReadAllLines(path, Encoding.Default);
}
///
/// 写入文件
///
///
///
///
///
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;
}
}
///
/// 写入文件
///
///
///
///
///
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);
}
///
/// 删除文件
///
///
///
public static bool Delete(string path)
{
try
{
File.Delete(path);
return true;
}
catch (Exception e)
{
LogSave.Add(e.Message);
return false;
}
}
///
/// 创建目录
///
///
///
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;
}
}
///
/// 将CSV文件的一行转换为列表,进行逗号分隔(字段内容中的逗号不参与分隔)
///
///
///
public static List CsvStrToList(string csvLine)
{
var newStr = string.Empty;
List sList = new List();
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;
}
}
}