基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
/******************************
|
||||
* 说明:AES对称加密
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-06-21
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// AES对称加密
|
||||
/// </summary>
|
||||
public class AESUtil
|
||||
{
|
||||
private static string key = "lserpasebygyc222";
|
||||
|
||||
#region AES加解密
|
||||
/// <summary>
|
||||
///AES加密(加密步骤)
|
||||
///1,加密字符串得到2进制数组;
|
||||
///2,将2进制数组转为16进制;
|
||||
///3,进行base64编码
|
||||
/// </summary>
|
||||
/// <param name="toEncrypt">要加密的字符串</param>
|
||||
/// <param name="key">密钥</param>
|
||||
public static string Encrypt(string toEncrypt)
|
||||
{
|
||||
if (string.IsNullOrEmpty(toEncrypt)) return "";
|
||||
byte[] _key = Encoding.ASCII.GetBytes(key);
|
||||
byte[] _source = Encoding.UTF8.GetBytes(toEncrypt);
|
||||
|
||||
Aes aes = Aes.Create("AES");
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
aes.Key = _key;
|
||||
ICryptoTransform cTransform = aes.CreateEncryptor();
|
||||
byte[] cryptData = cTransform.TransformFinalBlock(_source, 0, _source.Length);
|
||||
string hexCryptString = Hex_2To16(cryptData);
|
||||
byte[] hexCryptData = Encoding.UTF8.GetBytes(hexCryptString);
|
||||
return Convert.ToBase64String(hexCryptData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AES解密(解密步骤)
|
||||
/// 1,将BASE64字符串转为16进制数组
|
||||
/// 2,将16进制数组转为字符串
|
||||
/// 3,将字符串转为2进制数据
|
||||
/// 4,用AES解密数据
|
||||
/// </summary>
|
||||
/// <param name="encryptedSource">已加密的内容</param>
|
||||
/// <param name="key">密钥</param>
|
||||
public static string Decrypt(string toDecrypt)
|
||||
{
|
||||
if (string.IsNullOrEmpty(toDecrypt)) return "";
|
||||
try
|
||||
{
|
||||
byte[] _key = Encoding.ASCII.GetBytes(key);
|
||||
Aes aes = Aes.Create("AES");
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
aes.Key = _key;
|
||||
ICryptoTransform cTransform = aes.CreateDecryptor();
|
||||
|
||||
byte[] encryptedData = Convert.FromBase64String(toDecrypt);
|
||||
string encryptedString = Encoding.UTF8.GetString(encryptedData);
|
||||
byte[] _source = Hex_16To2(encryptedString);
|
||||
byte[] originalSrouceData = cTransform.TransformFinalBlock(_source, 0, _source.Length);
|
||||
return Encoding.UTF8.GetString(originalSrouceData);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 2进制转16进制
|
||||
/// </summary>
|
||||
static string Hex_2To16(byte[] bytes)
|
||||
{
|
||||
string hexString = string.Empty;
|
||||
Int32 iLength = 65535;
|
||||
if (bytes != null)
|
||||
{
|
||||
StringBuilder strB = new StringBuilder();
|
||||
|
||||
if (bytes.Length < iLength)
|
||||
{
|
||||
iLength = bytes.Length;
|
||||
}
|
||||
|
||||
for (int i = 0; i < iLength; i++)
|
||||
{
|
||||
strB.Append(bytes[i].ToString("X2"));
|
||||
}
|
||||
hexString = strB.ToString();
|
||||
}
|
||||
return hexString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 16进制转2进制
|
||||
/// </summary>
|
||||
static byte[] Hex_16To2(string hexString)
|
||||
{
|
||||
if ((hexString.Length % 2) != 0)
|
||||
{
|
||||
hexString += " ";
|
||||
}
|
||||
byte[] returnBytes = new Byte[hexString.Length / 2];
|
||||
for (Int32 i = 0; i < returnBytes.Length; i++)
|
||||
{
|
||||
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
||||
}
|
||||
return returnBytes;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,437 @@
|
||||
/******************************
|
||||
* 说明:本地日志帮助类
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2017-08-10
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.IO;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志类型
|
||||
/// </summary>
|
||||
public enum LogType
|
||||
{
|
||||
Error,
|
||||
Warning,
|
||||
Notice
|
||||
}
|
||||
/// <summary>
|
||||
/// 日志帮助类
|
||||
/// </summary>
|
||||
public class LogHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志文件存放文件夹
|
||||
/// </summary>
|
||||
private string _dicPath { get { return AppDomain.CurrentDomain.BaseDirectory + "Log"; } }
|
||||
private string _pattern = ".txt";
|
||||
/// <summary>
|
||||
/// 单个日志文件大小
|
||||
/// </summary>
|
||||
private int _fileSize = 1024*2048;
|
||||
/// <summary>
|
||||
/// 日志队列
|
||||
/// </summary>
|
||||
private Queue<string> _logQueue = new Queue<string>();
|
||||
/// <summary>
|
||||
/// 日志队列锁
|
||||
/// </summary>
|
||||
private object _queueLock = new object();
|
||||
private object _objLock = new object();
|
||||
/// <summary>
|
||||
/// 日志文件名列表
|
||||
/// </summary>
|
||||
private List<string> _fileList = new List<string>();
|
||||
|
||||
private static LogHelper instance = null;
|
||||
public static LogHelper Instance
|
||||
{
|
||||
get {
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new LogHelper();
|
||||
instance.Start();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
public LogHelper()
|
||||
{
|
||||
if(Directory.Exists(_dicPath))
|
||||
{
|
||||
_fileList.Clear();
|
||||
foreach (string fileName in Directory.GetFiles(_dicPath,"*"+_pattern))
|
||||
{
|
||||
string name = fileName.Substring(fileName.LastIndexOf("\\")+1);
|
||||
_fileList.Add(name);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Task.Factory.StartNew(()=> {
|
||||
WriteLogFunc();
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>说明:写入文件</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
private void WriteLogFunc()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
if (CurrentLogCount() > 0)
|
||||
{
|
||||
string strWriteLog = GetLog();
|
||||
WriteLogToFile(strWriteLog);
|
||||
}
|
||||
else
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
#region 日志队列使用方式
|
||||
/// <summary>
|
||||
/// <para>说明:加入日志</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="strLog">The string log.</param>
|
||||
private void AddLog(string strLog)
|
||||
{
|
||||
lock (_queueLock)
|
||||
{
|
||||
_logQueue.Enqueue(strLog);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:获取日志</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns>System.String.</returns>
|
||||
private string GetLog()
|
||||
{
|
||||
string strLog = "";
|
||||
lock (_queueLock)
|
||||
{
|
||||
if (_logQueue.Count > 0)
|
||||
strLog = _logQueue.Dequeue();
|
||||
}
|
||||
return strLog;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:当前队列条数</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns>System.Int32.</returns>
|
||||
private int CurrentLogCount()
|
||||
{
|
||||
int count = 0;
|
||||
lock (_queueLock)
|
||||
{
|
||||
count = _logQueue.Count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Method
|
||||
/// <summary>
|
||||
/// <para>说明:检查日志存放目录是否存在,如果不存在就创建</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if [is directory exist]; otherwise, <c>false</c>.</returns>
|
||||
private bool IsDirectoryExist()
|
||||
{
|
||||
if(!Directory.Exists(_dicPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(_dicPath);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:创建日志文件,如果已存在文件名就改变</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="filePath">The file path.</param>
|
||||
/// <returns><c>true</c> if [is create log file] [the specified file path]; otherwise, <c>false</c>.</returns>
|
||||
private bool IsCreateLogFile(ref string filePath)
|
||||
{
|
||||
if(File.Exists(filePath))
|
||||
{
|
||||
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
||||
int number = 1;
|
||||
string dateFileName = DateTime.Now.ToShortDateString().Replace('/', '_');
|
||||
while(true)
|
||||
{
|
||||
string tempName = string.Format("{0}.{1}{2}", dateFileName, number, _pattern);
|
||||
if (_fileList.Where(p => p == tempName).Count() == 0)
|
||||
break;
|
||||
number++;
|
||||
}
|
||||
filePath = _dicPath+"\\"+ string.Format("{0}.{1}{2}", dateFileName, number, _pattern);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
FileStream fs = File.Create(filePath, _fileSize);
|
||||
fs.Close();
|
||||
_fileList.Add(Path.GetFileName(filePath));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:检查写入的内容是否超过文件的大小限制</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="filePath">The file path.</param>
|
||||
/// <param name="writeLog">The write log.</param>
|
||||
/// <returns><c>true</c> if [is log content out of size] [the specified file path]; otherwise, <c>false</c>.</returns>
|
||||
private bool IsLogContentOutOfSize(string filePath,string writeLog)
|
||||
{
|
||||
FileInfo fInfo = new FileInfo(filePath);
|
||||
if(fInfo.Length + writeLog.Length < _fileSize)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:检查日志目录下有没有最新的日志文件,没有就创建新的日文件</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if [is last file exist]; otherwise, <c>false</c>.</returns>
|
||||
private bool IsLastFileExist()
|
||||
{
|
||||
if(_fileList.Count == 0)
|
||||
{
|
||||
string filePath = _dicPath + "\\" + DateTime.Now.ToShortDateString().Replace('/','_')+_pattern;
|
||||
try
|
||||
{
|
||||
FileStream fs = File.Create(filePath,_fileSize);
|
||||
fs.Close();
|
||||
_fileList.Add(Path.GetFileName(filePath));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:日志写入文件</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="writeLog">The write log.</param>
|
||||
private void WriteLogToFile(string writeLog)
|
||||
{
|
||||
if (!IsDirectoryExist())
|
||||
return;
|
||||
if (!IsLastFileExist())
|
||||
return;
|
||||
|
||||
string writeFilePath = _dicPath + "\\" + _fileList.Last();
|
||||
if (!File.Exists(_dicPath + "\\" + DateTime.Now.ToShortDateString().Replace('/', '_') + _pattern))
|
||||
{
|
||||
writeFilePath = _dicPath + "\\" + DateTime.Now.ToShortDateString().Replace('/', '_') + _pattern;
|
||||
if (!IsCreateLogFile(ref writeFilePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(IsLogContentOutOfSize(writeFilePath,writeLog))
|
||||
{
|
||||
if(!IsCreateLogFile(ref writeFilePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
FileStream fstream = null;
|
||||
StreamWriter streamWriter = null;
|
||||
try
|
||||
{
|
||||
fstream = new FileStream(writeFilePath,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);
|
||||
streamWriter = new StreamWriter(fstream,Encoding.UTF8);
|
||||
streamWriter.WriteLine(writeLog);
|
||||
streamWriter.Flush();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
streamWriter.Close();
|
||||
fstream.Close();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:写入日志</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-10 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="obj">The object.</param>
|
||||
/// <param name="logType">Type of the log.</param>
|
||||
public void WriteLog(object obj,LogType logType= LogType.Notice)
|
||||
{
|
||||
string strLog = "";
|
||||
lock(_objLock)
|
||||
{
|
||||
strLog = obj.ToString();
|
||||
}
|
||||
if (string.IsNullOrEmpty(strLog))
|
||||
return;
|
||||
string strWriteLog = string.Format("{0:yyyy-MM-dd HH:mm:ss:fff}", DateTime.Now) + " " +string.Format("[{0}]",logType)+ strLog + Environment.NewLine;
|
||||
AddLog(strWriteLog);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:写入日志</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-12 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="obj">The object.</param>
|
||||
/// <param name="title">The title.</param>
|
||||
/// <param name="logType">Type of the log.</param>
|
||||
public void WriteLog(object obj, string title, LogType logType = LogType.Notice)
|
||||
{
|
||||
string strLog = "";
|
||||
lock (_objLock)
|
||||
{
|
||||
strLog = obj.ToString();
|
||||
}
|
||||
if (string.IsNullOrEmpty(strLog))
|
||||
return;
|
||||
string strWriteLog = string.Format("{0:yyyy-MM-dd HH:mm:ss:fff}", DateTime.Now) + " " + string.Format("[{0}]", logType) + " " + string.Format("[{0}]", title) + strLog + Environment.NewLine;
|
||||
AddLog(strWriteLog);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:写入异常</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-12 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="obj">The object.</param>
|
||||
/// <param name="logType">Type of the log.</param>
|
||||
public void WriteError(Exception exception, LogType logType = LogType.Error)
|
||||
{
|
||||
string strLog = "";
|
||||
lock (_objLock)
|
||||
{
|
||||
strLog = exception.Message + "\r\n" + exception.StackTrace;
|
||||
}
|
||||
if (string.IsNullOrEmpty(strLog))
|
||||
return;
|
||||
string strWriteLog = string.Format("{0:yyyy-MM-dd HH:mm:ss:fff}", DateTime.Now) + " " + string.Format("[{0}]", logType) + strLog + Environment.NewLine;
|
||||
AddLog(strWriteLog);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:写入异常</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-08-12 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="exception">The exception.</param>
|
||||
/// <param name="title">The title.</param>
|
||||
/// <param name="logType">Type of the log.</param>
|
||||
public void WriteError(Exception exception, string title, LogType logType = LogType.Error)
|
||||
{
|
||||
string strLog = "";
|
||||
lock (_objLock)
|
||||
{
|
||||
strLog = exception.Message + "\r\n" + exception.StackTrace;
|
||||
}
|
||||
if (string.IsNullOrEmpty(strLog))
|
||||
return;
|
||||
string strWriteLog = string.Format("{0:yyyy-MM-dd HH:mm:ss:fff}", DateTime.Now) + " " + string.Format("[{0}]", logType) + " " + string.Format("[{0}]", title) + strLog + Environment.NewLine;
|
||||
AddLog(strWriteLog);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的常规信息通过以下
|
||||
// 特性集控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("Lskj.PubUtil")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Microsoft")]
|
||||
[assembly: AssemblyProduct("Lskj.PubUtil")]
|
||||
[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 使此程序集中的类型
|
||||
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
|
||||
// 则将该类型上的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("84181195-b760-494a-abc5-22abd601f349")]
|
||||
|
||||
// 程序集的版本信息由下面四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 内部版本号
|
||||
// 修订号
|
||||
//
|
||||
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
|
||||
// 方法是按如下所示使用“*”:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.1")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.1")]
|
||||
@@ -0,0 +1,146 @@
|
||||
/******************************
|
||||
* 说明:DataTable相关扩展方法
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2017-11-15
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// DataTable相关扩展方法
|
||||
/// </summary>
|
||||
public static class DataTableExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:移除DataTable中DataRow空白行</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-11-15 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="rows">The rows.</param>
|
||||
public static DataTable TrimEmptyRows(this DataTable table)
|
||||
{
|
||||
//GridView.DeleteSelectedRows()只是改变了数据状态为delete,数据依然存在于table中需要更新
|
||||
table.AcceptChanges();
|
||||
if (table == null) return table;
|
||||
|
||||
int rowCount = table.Rows.Count;
|
||||
for (int i = rowCount - 1; i > 0; i--)
|
||||
{
|
||||
bool isNull = true;
|
||||
DataRow item = table.Rows[i];
|
||||
for (int j = 0; j < table.Columns.Count; j++)
|
||||
{
|
||||
DataColumn col = table.Columns[j];
|
||||
if (!string.IsNullOrWhiteSpace(item[col.ColumnName] + ""))
|
||||
{
|
||||
isNull = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isNull)
|
||||
{
|
||||
table.Rows.Remove(item);
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:移除DataTable中已删除的行</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-11-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="table">The table.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable TrimDeleteRows(this DataTable table)
|
||||
{
|
||||
if (table == null || table.Rows.Count == 0) return table;
|
||||
|
||||
DataRow[] deleteRows = table.Rows.Cast<DataRow>().Where(x => x.RowState == DataRowState.Deleted).ToArray();
|
||||
|
||||
for (int i = 0; i < deleteRows.Length; i++)
|
||||
{
|
||||
table.Rows.Remove(deleteRows[i]);
|
||||
}
|
||||
return table;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:按field排序DataTable</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-11-30 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="table">The table.</param>
|
||||
/// <param name="field">The field.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable OrderBy(this DataTable table, string field)
|
||||
{
|
||||
if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field)) return table;
|
||||
|
||||
table.AcceptChanges();
|
||||
table = table.TrimDeleteRows();
|
||||
|
||||
for (int i = 0; i < table.Rows.Count; i++)
|
||||
{
|
||||
table.Rows[i][field] = (i + 1);
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:按配置条件排序DataTable</para>
|
||||
/// <para>创建人:王一帆</para>
|
||||
/// <para>创建日期:2020-07-03 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="table">The table.</param>
|
||||
/// <param name="field">The field.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable OrderBy(this DataTable table, string field, string UpperFieldName, int RowHandle)
|
||||
{
|
||||
if (table == null || table.Rows.Count == 0 || string.IsNullOrEmpty(field) || !table.Columns.Contains(field)) return table;
|
||||
|
||||
table.AcceptChanges();
|
||||
table = table.TrimDeleteRows();
|
||||
if (RowHandle == 0)
|
||||
{
|
||||
for (int i = RowHandle; i < table.Rows.Count; i++)
|
||||
{
|
||||
if (string.IsNullOrEmpty(table.Rows[i][UpperFieldName] + "")) continue;
|
||||
table.Rows[i+1][field] = table.Rows[i][UpperFieldName];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = RowHandle; i < table.Rows.Count; i++)
|
||||
{
|
||||
if (string.IsNullOrEmpty(table.Rows[i - 1][UpperFieldName] + "")) continue;
|
||||
table.Rows[i][field] = table.Rows[i - 1][UpperFieldName];
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
public static class ListExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:集合转为DataTable</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-06-11 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="collection">The collection.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable ToDataTable<T>(this List<T> list)
|
||||
{
|
||||
IEnumerable<T> collection= list.AsEnumerable();
|
||||
var props = typeof(T).GetProperties();
|
||||
var dt = new DataTable();
|
||||
dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
|
||||
if (collection.Count() > 0)
|
||||
{
|
||||
for (int i = 0; i < collection.Count(); i++)
|
||||
{
|
||||
ArrayList tempList = new ArrayList();
|
||||
foreach (PropertyInfo pi in props)
|
||||
{
|
||||
object obj = pi.GetValue(collection.ElementAt(i), null);
|
||||
tempList.Add(obj);
|
||||
}
|
||||
object[] array = tempList.ToArray();
|
||||
dt.LoadDataRow(array, true);
|
||||
}
|
||||
}
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/******************************
|
||||
* 说明:字符串扩展
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-05-28
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
public static class StringExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:字符串转换为DataTable</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-05-28 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="stringData">The string data.</param>
|
||||
/// <param name="columns">The columns.</param>
|
||||
/// <returns>DataTable.</returns>
|
||||
public static DataTable ToNewDataTable(this string stringData,string columns)
|
||||
{
|
||||
DataTable dtTable = new DataTable();
|
||||
stringData = stringData.Replace(" ", "").Replace(",,", ",").Trim(',');
|
||||
string[] confirmOper = stringData.Split(',');
|
||||
for (int i = 0; i < confirmOper.Length; i++)
|
||||
{
|
||||
DataRow row = dtTable.NewRow();
|
||||
row[columns] = confirmOper[i];
|
||||
dtTable.Rows.Add(row);
|
||||
}
|
||||
return dtTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>说明:</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-10-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="s">The s.</param>
|
||||
/// <param name="format">The format.</param>
|
||||
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
|
||||
public static string ToStr(this object s, string format = "")
|
||||
{
|
||||
string result = string.Empty;
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(format))
|
||||
{
|
||||
result = s.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = string.Format("{0:" + format + "}", s);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:{key}字符串替换</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-05-28 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="ignoreCase">是否忽略大小写</param>
|
||||
/// <returns>System.String.</returns>
|
||||
public static string Replace(this string str, string key, string value, bool ignoreCase)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str)) return str;
|
||||
|
||||
return Regex.IsMatch(str, key, RegexOptions.IgnoreCase) ?
|
||||
System.Text.RegularExpressions.Regex.Replace(str, key, value, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None)
|
||||
: str;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期: </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
public static bool IsChinese(this string text)
|
||||
{
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
if ((int)text[i] > 127)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user