File name
Commit message
Commit date
File name
Commit message
Commit date
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace KHModbus
{
public class U3Util
{
static public Random rnd = new Random(new System.DateTime().Millisecond);
static public string GetErrorLogFile(bool bFullPath)
{
string AppPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"));
string AppName = Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf("\\") + 1);
string AppTitle = AppName.Substring(0, AppName.LastIndexOf("."));
if (bFullPath)
return AppPath + "\\" + AppTitle + "Error.txt";
else
return AppTitle + "Error.txt";
}
static public string GetEventLogFile(bool bFullPath)
{
string AppPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"));
string AppName = Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf("\\") + 1);
string AppTitle = AppName.Substring(0, AppName.LastIndexOf("."));
if (bFullPath)
return AppPath + "\\" + AppTitle + "Event.txt";
else
return AppTitle + "Event.txt";
}
static public string GetImagePath()
{
string AppPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"));
return AppPath + "\\" + "Images";
}
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
public static void ErrorLog(string strLog)
{
try
{
FileLog(strLog, GetErrorLogFile(false));
}
catch
{
}
}
public static void EventLog(string strLog)
{
try
{
FileLog(strLog, GetEventLogFile(false));
}
catch
{
}
}
public static void FileLog(string strLog, string strFileName)
{
string str = "";
DateTime t = DateTime.Now;
str = t.ToString("(yyyy-MM-dd HH:mm:ss.fff) ");
str += strLog;
try
{
string AppPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"));
string strLogFile = AppPath + "\\" + strFileName;
FileBackup(strLogFile);
FileWrite(strLogFile, str);
}
catch (Exception ex)
{
throw new Exception("FileLog->:" + ex.Message);
}
}
public static void FileBackup(string strLogFile)
{
if (File.Exists(strLogFile))
{
try
{
FileInfo f = new FileInfo(strLogFile);
if (f.Length >= (2 * 1024 * 1024))
{
System.IO.File.Delete(strLogFile + ".bak");
f.MoveTo(strLogFile + ".bak");
}
}
catch (Exception ex)
{
throw new Exception("FileBackup->:" + ex.Message);
}
}
}
public static void FileWrite(string strLogFile, string str)
{
try
{
FileStream fs = new FileStream(strLogFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
fs.Seek(0, SeekOrigin.End);
StreamWriter swFromFile = new StreamWriter(fs);
swFromFile.WriteLine(str);
swFromFile.Close();
fs.Close();
}
catch (Exception ex)
{
ErrorLog("FileWrite->:" + ex.Message);
}
}
public static string FileRead(string strFileName)
{
if (System.IO.File.Exists(strFileName) == false) return "";
string result = "";
try
{
FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
fs.Seek(0, SeekOrigin.Begin);
StreamReader srFile = new StreamReader(fs, Encoding.Default, true);
result = srFile.ReadToEnd();
srFile.Close();
fs.Close();
}
catch (Exception ex)
{
ErrorLog("FileRead->:" + ex.Message);
}
return result;
}
public static bool GetBit(byte b, int bitNumber)
{
return (b & (1 << bitNumber)) != 0;
}
public static bool GetBit(short s, int bitNumber)
{
return (s & (1 << bitNumber)) != 0;
}
static public DateTime FromUnixTime(long unixTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(unixTime);
}
static public Int32 ToUnixTime(DateTime dt)
{
return (Int32)(dt.Subtract(new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
}
static public void DeleteAllFile(string strPath)
{
try
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(strPath);
FileInfo[] lst = di.GetFiles();
foreach (FileInfo fi in lst)
{
System.IO.File.Delete(fi.FullName);
}
}
catch
{
}
}
public static string toStr(object obj, string def = "")
{
if (obj == null) return def;
try
{
return obj.ToString().Trim();
}
catch
{
return def;
}
}
public static bool toBoolean(object obj, bool def = false)
{
if (obj == null) return def;
try
{
return (bool)obj;
}
catch
{
return def;
}
}
public static DateTime toDateTime(object obj)
{
try
{
return Convert.ToDateTime(obj);
}
catch
{
return DateTime.MinValue;
}
}
public static Decimal toDecimal(object obj, Decimal def = 0M)
{
if (obj == null) return def;
try
{
return Convert.ToDecimal(obj);
}
catch
{
return def;
}
}
public static int toInt(object A, int def = 0)
{
if (A == null) return def;
try
{
return Convert.ToInt32(Math.Round(Convert.ToDouble(U3Util.toStr(A, "")), 0));
}
catch
{
return def;
}
}
public static double toDouble(object A, double def = 0.0)
{
if (A == null) return def;
try
{
return double.Parse(U3Util.toStr(A, ""));
}
catch
{
return def;
}
}
public static float toFloat(object A, float def = 0.0f)
{
if (A == null) return def;
try
{
return float.Parse(U3Util.toStr(A, ""));
}
catch
{
return def;
}
}
}
}