添加项目文件。
This commit is contained in:
1 parent
8cba828b45
commit
fea866f74f
24 files changed
+1503
No files matched your search
@@ -0,0 +1,190 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
/// <summary>
|
||||
/// CRC校验
|
||||
/// </summary>
|
||||
public class CRCcheck16
|
||||
{
|
||||
|
||||
#region CRC16
|
||||
public static byte[] CRC16(byte[] data)
|
||||
{
|
||||
int len = data.Length;
|
||||
if (len > 0)
|
||||
{
|
||||
ushort crc = 0xFFFF;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
crc = (ushort)(crc ^ (data[i]));
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
|
||||
}
|
||||
}
|
||||
byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
|
||||
byte lo = (byte)(crc & 0x00FF); //低位置
|
||||
|
||||
return new byte[] { hi, lo };
|
||||
}
|
||||
return new byte[] { 0, 0 };
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ToCRC16
|
||||
public static string ToCRC16(string content)
|
||||
{
|
||||
return ToCRC16(content, Encoding.UTF8);
|
||||
}
|
||||
|
||||
public static string ToCRC16(string content, bool isReverse)
|
||||
{
|
||||
return ToCRC16(content, Encoding.UTF8, isReverse);
|
||||
}
|
||||
|
||||
public static string ToCRC16(string content, Encoding encoding)
|
||||
{
|
||||
return ByteToString(CRC16(encoding.GetBytes(content)), true);
|
||||
}
|
||||
|
||||
public static string ToCRC16(string content, Encoding encoding, bool isReverse)
|
||||
{
|
||||
return ByteToString(CRC16(encoding.GetBytes(content)), isReverse);
|
||||
}
|
||||
|
||||
public static string ToCRC16(byte[] data)
|
||||
{
|
||||
return ByteToString(CRC16(data), true);
|
||||
}
|
||||
|
||||
public static string ToCRC16(byte[] data, bool isReverse)
|
||||
{
|
||||
return ByteToString(CRC16(data), isReverse);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ToModbusCRC16
|
||||
public static string ToModbusCRC16(string s)
|
||||
{
|
||||
return ToModbusCRC16(s, true);
|
||||
}
|
||||
|
||||
public static string ToModbusCRC16(string s, bool isReverse)
|
||||
{
|
||||
return ByteToString(CRC16(StringToHexByte(s)), isReverse);
|
||||
}
|
||||
|
||||
public static string ToModbusCRC16(byte[] data)
|
||||
{
|
||||
return ToModbusCRC16(data, true);
|
||||
}
|
||||
|
||||
public static string ToModbusCRC16(byte[] data, bool isReverse)
|
||||
{
|
||||
return ByteToString(CRC16(data), isReverse);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ByteToString
|
||||
public static string ByteToString(byte[] arr, bool isReverse)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte hi = arr[0], lo = arr[1];
|
||||
return Convert.ToString(isReverse ? hi + lo * 0x100 : hi * 0x100 + lo, 16).ToUpper().PadLeft(4, '0');
|
||||
}
|
||||
catch (Exception ex) { throw (ex); }
|
||||
}
|
||||
|
||||
public static string ByteToString(byte[] arr)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ByteToString(arr, true);
|
||||
}
|
||||
catch (Exception ex) { throw (ex); }
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region StringToHexString
|
||||
public static string StringToHexString(string str)
|
||||
{
|
||||
StringBuilder s = new StringBuilder();
|
||||
foreach (short c in str.ToCharArray())
|
||||
{
|
||||
s.Append(c.ToString("X4"));
|
||||
}
|
||||
return s.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region StringToHexByte
|
||||
private static string ConvertChinese(string str)
|
||||
{
|
||||
StringBuilder s = new StringBuilder();
|
||||
foreach (short c in str.ToCharArray())
|
||||
{
|
||||
if (c <= 0 || c >= 127)
|
||||
{
|
||||
s.Append(c.ToString("X4"));
|
||||
}
|
||||
else
|
||||
{
|
||||
s.Append((char)c);
|
||||
}
|
||||
}
|
||||
return s.ToString();
|
||||
}
|
||||
|
||||
private static string FilterChinese(string str)
|
||||
{
|
||||
StringBuilder s = new StringBuilder();
|
||||
foreach (short c in str.ToCharArray())
|
||||
{
|
||||
if (c > 0 && c < 127)
|
||||
{
|
||||
s.Append((char)c);
|
||||
}
|
||||
}
|
||||
return s.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字符串转16进制字符数组
|
||||
/// </summary>
|
||||
/// <param name="hex"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] StringToHexByte(string str)
|
||||
{
|
||||
return StringToHexByte(str, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字符串转16进制字符数组
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="isFilterChinese">是否过滤掉中文字符</param>
|
||||
/// <returns></returns>
|
||||
public static byte[] StringToHexByte(string str, bool isFilterChinese)
|
||||
{
|
||||
string hex = isFilterChinese ? FilterChinese(str) : ConvertChinese(str);
|
||||
|
||||
//清除所有空格
|
||||
hex = hex.Replace(" ", "");
|
||||
//若字符个数为奇数,补一个0
|
||||
hex += hex.Length % 2 != 0 ? "0" : "";
|
||||
|
||||
byte[] result = new byte[hex.Length / 2];
|
||||
for (int i = 0, c = result.Length; i < c; i++)
|
||||
{
|
||||
result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
/// <summary>
|
||||
/// 实时更新datagrid
|
||||
/// 调用示例:DataGridHelper.SetRealTimeCommit(Grid, true); Grid为对象控件名
|
||||
/// </summary>
|
||||
public static class DataGridHelper
|
||||
{
|
||||
public static void SetRealTimeCommit(DataGrid dataGrid, bool isRealTime)
|
||||
{
|
||||
dataGrid.SetValue(RealTimeCommitProperty, isRealTime);
|
||||
}
|
||||
|
||||
public static bool GetRealTimeCommit(DataGrid dataGrid)
|
||||
{
|
||||
return (bool)dataGrid.GetValue(RealTimeCommitProperty);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty RealTimeCommitProperty =
|
||||
DependencyProperty.RegisterAttached("RealTimeCommit", typeof(bool),
|
||||
typeof(DataGridHelper),
|
||||
new PropertyMetadata(false, RealTimeCommitCallBack));
|
||||
|
||||
private static void RealTimeCommitCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var dg = d as DataGrid;
|
||||
if (dg == null)
|
||||
return;
|
||||
EventHandler<DataGridCellEditEndingEventArgs> ceHandler = delegate (object xx, DataGridCellEditEndingEventArgs yy)
|
||||
{
|
||||
var flag = GetRealTimeCommit(dg);
|
||||
if (!flag)
|
||||
return;
|
||||
var cellContent = yy.Column.GetCellContent(yy.Row);
|
||||
if (cellContent != null && cellContent.BindingGroup != null)
|
||||
cellContent.BindingGroup.CommitEdit();
|
||||
};
|
||||
dg.CellEditEnding += ceHandler;
|
||||
RoutedEventHandler eh = null;
|
||||
eh = (xx, yy) =>
|
||||
{
|
||||
dg.Unloaded -= eh;
|
||||
dg.CellEditEnding -= ceHandler;
|
||||
};
|
||||
dg.Unloaded += eh;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
internal class DataTableToObservableCollection //数据表到可观察集合
|
||||
{
|
||||
public ObservableCollection<T> ToObservableCollection<T>(DataTable dt) where T : class, new()
|
||||
{
|
||||
Type t = typeof(T);
|
||||
PropertyInfo[] propertys = t.GetProperties();
|
||||
ObservableCollection<T> lst = new ObservableCollection<T>();
|
||||
string typeName = string.Empty;
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
T entity = new T();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
typeName = pi.Name;
|
||||
if (dt.Columns.Contains(typeName))
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
object value = dr[typeName];
|
||||
if (value == DBNull.Value) continue;
|
||||
if (pi.PropertyType == typeof(string))
|
||||
{
|
||||
pi.SetValue(entity, value.ToString(), null);
|
||||
}
|
||||
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
|
||||
{
|
||||
pi.SetValue(entity, int.Parse(value.ToString()), null);
|
||||
}
|
||||
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
|
||||
{
|
||||
pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
|
||||
}
|
||||
else if (pi.PropertyType == typeof(float))
|
||||
{
|
||||
pi.SetValue(entity, float.Parse(value.ToString()), null);
|
||||
}
|
||||
else if (pi.PropertyType == typeof(double))
|
||||
{
|
||||
pi.SetValue(entity, double.Parse(value.ToString()), null);
|
||||
}
|
||||
else
|
||||
{
|
||||
pi.SetValue(entity, value, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
lst.Add(entity);
|
||||
}
|
||||
return lst;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using System.Management;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取硬件SN
|
||||
/// </summary>
|
||||
internal class HardwareSN
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取cpuid
|
||||
/// </summary>
|
||||
public static string GetCPUSerialNumber()
|
||||
{
|
||||
try
|
||||
{
|
||||
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
|
||||
string cpuSerialNumber = "";
|
||||
foreach (ManagementObject mo in searcher.Get())
|
||||
{
|
||||
cpuSerialNumber = mo["ProcessorId"].ToString().Trim();
|
||||
break;
|
||||
}
|
||||
return cpuSerialNumber;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取主板id
|
||||
/// </summary>
|
||||
public static string GetBIOSSerialNumber()
|
||||
{
|
||||
try
|
||||
{
|
||||
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
|
||||
string biosSerialNumber = "";
|
||||
foreach (ManagementObject mo in searcher.Get())
|
||||
{
|
||||
biosSerialNumber = mo.GetPropertyValue("SerialNumber").ToString().Trim();
|
||||
break;
|
||||
}
|
||||
return biosSerialNumber;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取硬盘id
|
||||
/// </summary>
|
||||
public static string GetHardDiskSerialNumber()
|
||||
{
|
||||
try
|
||||
{
|
||||
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
|
||||
string hardDiskSerialNumber = "";
|
||||
foreach (ManagementObject mo in searcher.Get())
|
||||
{
|
||||
hardDiskSerialNumber = mo["SerialNumber"].ToString().Trim();
|
||||
break;
|
||||
}
|
||||
return hardDiskSerialNumber;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取网卡id
|
||||
/// </summary>
|
||||
public static string GetNetCardMACAddress()
|
||||
{
|
||||
try
|
||||
{
|
||||
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");
|
||||
string netCardMACAddress = "";
|
||||
foreach (ManagementObject mo in searcher.Get())
|
||||
{
|
||||
netCardMACAddress = mo["MACAddress"].ToString().Trim();
|
||||
break;
|
||||
}
|
||||
return netCardMACAddress;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
internal class IniFile
|
||||
{
|
||||
public class IniFiles
|
||||
{
|
||||
public string path;
|
||||
[DllImport("kernel32")] //返回0表示失败,非0为成功
|
||||
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
|
||||
[DllImport("kernel32")] //返回取得字符串缓冲区的长度
|
||||
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
|
||||
/// <summary>
|
||||
/// 保存ini文件的路径
|
||||
/// 调用示例:var ini = IniFiles("C:\file.ini");
|
||||
/// </summary>
|
||||
/// <param name="INIPath"></param>
|
||||
public IniFiles(string iniPath)
|
||||
{
|
||||
this.path = iniPath;
|
||||
}
|
||||
/// <summary>
|
||||
/// 写Ini文件
|
||||
/// 调用示例:ini.IniWritevalue("Server","name","localhost");
|
||||
/// </summary>
|
||||
/// <param name="Section">[缓冲区]</param>
|
||||
/// <param name="Key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public void IniWritevalue(string Section, string Key, string value)
|
||||
{
|
||||
WritePrivateProfileString(Section, Key, value, this.path);
|
||||
}
|
||||
/// <summary>
|
||||
/// 读Ini文件
|
||||
/// 调用示例:ini.IniWritevalue("Server","name");
|
||||
/// </summary>
|
||||
/// <param name="Section">[缓冲区]</param>
|
||||
/// <param name="Key">键</param>
|
||||
/// <returns>值</returns>
|
||||
public string IniReadvalue(string Section, string Key)
|
||||
{
|
||||
StringBuilder temp = new StringBuilder(255);
|
||||
|
||||
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
|
||||
return temp.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System.Text;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
public static class LogDataRead
|
||||
{
|
||||
private const int MaxCount = 1000;
|
||||
private static int Count = 0;
|
||||
private static RichTextBox textControl;
|
||||
private static InlineCollection inlines;
|
||||
|
||||
//设置主控件
|
||||
public static void SetTextControl(RichTextBox _textBox)
|
||||
{
|
||||
textControl = _textBox;
|
||||
Paragraph graph = new Paragraph();
|
||||
inlines = graph.Inlines;
|
||||
textControl.Document.Blocks.Add(graph);
|
||||
}
|
||||
|
||||
//输出黑色消息
|
||||
public static void Info(string format, params object[] args)
|
||||
{
|
||||
AppendText(Brushes.Black, format, args);
|
||||
}
|
||||
|
||||
//输出绿色消息
|
||||
public static void Suc(string format, params object[] args)
|
||||
{
|
||||
AppendText(Brushes.DarkGreen, format, args);
|
||||
}
|
||||
|
||||
//输出黄色消息
|
||||
public static void Warning(string format, params object[] args)
|
||||
{
|
||||
AppendText(Brushes.DarkOrange, format, args);
|
||||
}
|
||||
|
||||
//输出红色消息
|
||||
public static void Error(string format, params object[] args)
|
||||
{
|
||||
AppendText(Brushes.Red, format, args);
|
||||
}
|
||||
|
||||
//清除日志
|
||||
public static void Clear()
|
||||
{
|
||||
Count = 0;
|
||||
inlines.Clear();
|
||||
textControl.ScrollToEnd();
|
||||
}
|
||||
|
||||
private static void AppendText(Brush color, string format, params object[] args)
|
||||
{
|
||||
textControl.BeginChange();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.Append("[");
|
||||
builder.Append(Count++);
|
||||
builder.Append("] : ");
|
||||
builder.Append(string.Format(format, (object[])args));
|
||||
builder.Append("\n");
|
||||
string str = builder.ToString();
|
||||
inlines.Add(new Run(str) { Foreground = color });
|
||||
if (inlines.Count > MaxCount)
|
||||
{
|
||||
inlines.Remove(inlines.FirstInline);
|
||||
}
|
||||
textControl.ScrollToEnd();
|
||||
textControl.EndChange();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
public class LogGing
|
||||
{
|
||||
public static void LogGingDATA(string dat)
|
||||
{
|
||||
string logpath = System.Environment.CurrentDirectory + "\\Log";//日志文件目录
|
||||
string logPath = "" + System.Environment.CurrentDirectory + "\\Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//日志文件
|
||||
string Log_time = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:";
|
||||
|
||||
if (Directory.Exists(logpath))//检查日志路径
|
||||
{
|
||||
if (!File.Exists(logPath))//检查日志文件并写入启动日志
|
||||
{
|
||||
FileStream fs = new FileStream(logPath, FileMode.CreateNew, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine(Log_time + dat);
|
||||
wr.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write);
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine(Log_time + dat);
|
||||
wr.Close(); }
|
||||
}
|
||||
else
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(logpath);
|
||||
directoryInfo.Create();//创建日志路径
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
/// <summary>
|
||||
/// MD5加密
|
||||
/// </summary>
|
||||
internal class MD5check
|
||||
{
|
||||
/// <summary>
|
||||
/// 16位MD5加密
|
||||
/// </summary>
|
||||
/// <param name="password"></param>
|
||||
/// <returns></returns>
|
||||
public static string MD5Encrypt16(string password)
|
||||
{
|
||||
var md5 = new MD5CryptoServiceProvider();
|
||||
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8);
|
||||
t2 = t2.Replace("-", "");
|
||||
return t2;
|
||||
}
|
||||
/// <summary>
|
||||
/// 32位MD5加密
|
||||
/// </summary>
|
||||
/// <param name="password"></param>
|
||||
/// <returns></returns>
|
||||
public static string MD5Encrypt32(string password)
|
||||
{
|
||||
string cl = password;
|
||||
string pwd = "";
|
||||
MD5 md5 = MD5.Create(); //实例化一个md5对像
|
||||
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
|
||||
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
|
||||
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
|
||||
pwd = pwd + s[i].ToString("X");
|
||||
}
|
||||
return pwd;
|
||||
}
|
||||
/// <summary>
|
||||
/// 64位MD5加密
|
||||
/// </summary>
|
||||
/// <param name="password"></param>
|
||||
/// <returns></returns>
|
||||
public static string MD5Encrypt64(string password)
|
||||
{
|
||||
string cl = password;
|
||||
//string pwd = "";
|
||||
MD5 md5 = MD5.Create(); //实例化一个md5对像
|
||||
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
|
||||
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
|
||||
return Convert.ToBase64String(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
internal class PressKey
|
||||
{
|
||||
/// <summary>
|
||||
/// 键盘模拟
|
||||
/// </summary>
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
||||
public static void PressKeys(Keys key, bool up)
|
||||
{
|
||||
const int KEYEVENTF_EXTENDEDKEY = 0x1;
|
||||
const int KEYEVENTF_KEYUP = 0x2;
|
||||
if (up)
|
||||
{
|
||||
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
|
||||
}
|
||||
else
|
||||
{
|
||||
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串转数字
|
||||
/// </summary>
|
||||
internal class StrToInt
|
||||
{
|
||||
/// <summary>
|
||||
/// 十六进制字符串转十进制
|
||||
/// </summary>
|
||||
/// <param name="str">十六进制字符</param>
|
||||
/// <returns></returns>
|
||||
public static int To16Convert10(string str)
|
||||
{
|
||||
int res = 0;
|
||||
try
|
||||
{
|
||||
str = str.Trim().Replace(" ", ""); //移除空字符
|
||||
//方法1
|
||||
res = int.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier);
|
||||
//方法2
|
||||
//int r2 = System.Int32.Parse(str, System.Globalization.NumberStyles.HexNumber);
|
||||
//Console.WriteLine(r2);
|
||||
//方法3
|
||||
//int r3 = Convert.ToInt32(str, 16);
|
||||
//Console.WriteLine(r3);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
res = 0;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
public class TCPServer
|
||||
{
|
||||
public static string infoR;
|
||||
//第一步:调用socket()函数创建一个用于通信的套接字
|
||||
public static Socket listenSocket;
|
||||
|
||||
//字典集合:存储IP和Socket的集合
|
||||
public static Dictionary<string, Socket> OnLineList = new Dictionary<string, Socket>();
|
||||
|
||||
|
||||
//当前时间
|
||||
private string CurrentTime
|
||||
{
|
||||
get { return DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine; }
|
||||
}
|
||||
|
||||
//编码格式
|
||||
public static Encoding econding = Encoding.UTF8;
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
//第一步:调用socket()函数创建一个用于通信的套接字
|
||||
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
//第二步:给已经创建的套接字绑定一个端口号,这一般通过设置网络套接口地址和调用Bind()函数来实现
|
||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(UserClass.get_local_ip_address.IP_Address()), int.Parse("11080"));
|
||||
UserClass.LogGing.LogGingDATA("NativeIP = " + UserClass.get_local_ip_address.IP_Address());
|
||||
UserClass.LogGing.LogGingDATA("NativePORT = 11080");
|
||||
try
|
||||
{
|
||||
listenSocket.Bind(endPoint);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
UserClass.LogGing.LogGingDATA("TCP_NotStart");
|
||||
UserClass.LogGing.LogGingDATA(ex.Message);
|
||||
return;
|
||||
}
|
||||
//第三步:调用listen()函数使套接字成为一个监听套接字
|
||||
listenSocket.Listen(10);
|
||||
//ShowMessage("服务器开启成功");
|
||||
|
||||
//开启一个线程监听
|
||||
Task.Run(new Action(() => { ListenConnection(); }));
|
||||
}
|
||||
|
||||
public static void ListenConnection()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Socket clientSocket = listenSocket.Accept();
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
//更新在线列表
|
||||
// AddOnLine(ip, true);
|
||||
//更新在线列表集合
|
||||
OnLineList.Add(ip, clientSocket);
|
||||
UserClass.LogGing.LogGingDATA("IP_Online = " + ip);
|
||||
// ShowMessage(ip + "上线了");
|
||||
Task.Run(() => ReceiveMsg(clientSocket));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收方法
|
||||
/// </summary>
|
||||
/// <param name="clientSocket"></param>
|
||||
public static void ReceiveMsg(Socket clientSocket)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
//定义一个2M的缓冲区
|
||||
byte[] buffer = new byte[1024 * 1024 * 10];
|
||||
|
||||
int length = -1;
|
||||
try
|
||||
{
|
||||
length = clientSocket.Receive(buffer);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//客户端下线了
|
||||
//更新在线列表
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
UserClass.LogGing.LogGingDATA("IP_Line = " + ip);
|
||||
// AddOnLine(ip, false);
|
||||
OnLineList.Remove(ip); //移除ip
|
||||
break; //退出循环
|
||||
}
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
//客户端下线了
|
||||
//更新在线列表
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
UserClass.LogGing.LogGingDATA("IP_Line = " + ip);
|
||||
// AddOnLine(ip, false);
|
||||
OnLineList.Remove(ip);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
infoR = econding.GetString(buffer, 0, length);
|
||||
//ShowMessage(info);
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
/// <summary>
|
||||
/// 在线列表更新
|
||||
/// </summary>
|
||||
/// <param name="clientIp"></param>
|
||||
/// <param name="value"></param>
|
||||
private void AddOnLine(string clientIp, bool value)
|
||||
{
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
this.lst_Online.Items.Add(clientIp);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.lst_Online.Items.Remove(clientIp);
|
||||
}
|
||||
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新接收区
|
||||
/// </summary>
|
||||
/// <param name="info"></param>
|
||||
private void ShowMessage(string info)
|
||||
{
|
||||
Invoke(new Action(() =>
|
||||
{
|
||||
this.txt_Rcv.AppendText(CurrentTime + info + Environment.NewLine);
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
/// <summary>
|
||||
/// 消息发送
|
||||
/// </summary>
|
||||
public static void Bn_Send(string IP, string DAT)
|
||||
{
|
||||
if (OnLineList.ContainsKey(IP))
|
||||
{
|
||||
OnLineList[IP].Send(econding.GetBytes(DAT));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 群发功能
|
||||
/// </summary>
|
||||
public static void Btn_SendAll(string DAT)
|
||||
{
|
||||
foreach (string item in OnLineList.Keys.ToArray())
|
||||
{
|
||||
if (OnLineList.ContainsKey(item))
|
||||
{
|
||||
OnLineList[item].Send(econding.GetBytes(DAT));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace formula_manage.UserClass
|
||||
{
|
||||
public class get_local_ip_address
|
||||
{
|
||||
public static string getLocalIPAddressWithNetworkInterface(NetworkInterfaceType _type)//获取本机ip并以字符串方式返回
|
||||
{
|
||||
string output = "";
|
||||
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
|
||||
{
|
||||
if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
|
||||
{
|
||||
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
|
||||
{
|
||||
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
output = ip.Address.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static string IP_Address()
|
||||
{
|
||||
string ip;
|
||||
|
||||
ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Ethernet);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Wireless80211);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Ppp);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Wwanpp);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.TokenRing);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Fddi);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.GenericModem);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.IPOverAtm);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Tunnel);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Unknown);
|
||||
|
||||
if (ip == "") ip = "127.0.0.1";
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user