C#基础

数据类型

string int double long char byte

可空类型

int? i = 3;

Nullable i = new Nullable(3);

参数数组 params关键字

使用params关键字,在传递参数时,既可以传数组,也可以传数组参数

public int AddElements(params int[] arr)

@在c#中为强制不转义的符号,在里面的转义字符无效。

容器类型

public static T ToEntity<T>(this string json)

this关键字

string的使用及常见方法

string fname,lname;
fname = "Rowan";
lname = "Atkinson";
string fullnamme = fname+lname;
Console.WriteLine("Full Name:{0}", fullname);

char lettes = {'H','e','l','l','o'};
string greetings = new string(lettes);

比较两个string对象,并返回一个表示它们在排列顺序中相对位置的整数,该方法区分大小写
static int Compare(string strA, string strB)
如果不区分大小写
static int Compare(string strA, string strB, bool ignoreCase=true)
连接两个string,返回合并后的内容
static string Concat(string str0, string str1)
有连接三个、四个的重载方法

指定string对象中是否出现在字符串中
bool Contains(string value)

创建一个与指定字符串具有相同值得新的string对象
string Copy(string str)

复制string对象指定位置开始的值到字符数组的指定位置
void CopyTo(int sourceIndex, char[] destination, int destinationIndex,  int count)

//比较字符串是否相等
bool Equals(string value)

//字符串格式化
string Format("",...)

//字符串查找
int indexOf(char value)
int indexOf(string value)

从指定位置查找
int indexOf(char[] value, int startIndex)
int indexOf(string value, int startIndex)
int indexOfAny(char[] anyOf)

返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。
int insert(int startIndex, string value)

指示指定的字符串是否为null 或者是否为一个空的字符串
static bool IsNullOrEmpty(string value)

连接一个字符串数组中的所有元素,使用指定的分隔符分隔每个元素
static string Join(string separator, string[] value)
连接 指定位置开始,指定连接数量
static string Join(string separator, string[] value, int startIndex, int count)

//从字符串末尾搜索字符串第一次出现的位置
int LastIndexOf(char value)
int LastIndexOf(string value)

//移除当前实例中的所有字符,从指定位置开始,一直到最后一个位置为止,并返回字符串
string Remove(int startIndex)
string Remove(int startIndex, int count)

//字符串替换
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)

//字符串分割===========
string[] Split(params char[] separator)
指定最大数量
string[] Split(params char[] separator, int count)

//正则替换

//比较前缀
bool StartWith(string value)

//比较后缀
bool EndsWith(string value)

//传gbk编码
byte[] buffer = Encoding.GetEncoding("GBK").GetBytes(str)
//转utf8编码
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(str)
//转char数组
bool ToCharArray()
bool ToCharArray(int startIndex, int length)

//将字符串转换为小写
string ToLower()
//将字符串转换为大写
string ToUpper()
//去除前导空格和后置空格
string Trim()

常见的类

Array

//逆转数组
Array.Reverse(list)
//排序数组
var list = {34,72,13,44,25,30,10}
Array.Sort(list)

LIst

List ListOfT = new List();

List<Person> persons = new List<Person>();
persons.Add(p1);
persons.Add(p2);
persons.Add(p3);
persons.Remove("name");
persons.Count();
persons.Clear();
persons.RemoveAt(0);

交错数组 int[][] sources;

int [][] scores = new int[5][]; //创建第一维
for(int i=0; i<scores.length;i++){ 
   scores[i] = new int[4]; //遍历创建第二维
}

Hashable

Hashtable table = new Hashtable();
table.Add("name","yingzi");
table.Add("arg","0");
table.Add("datetime","2021-12-24")

table.Remove("name");
table.Clear();
//判断是否存在指定的键
table.Contains("name");

Object

//所有类默认继承Object类,包含Equals GetHashCode ToString Copy
 class Point
    {
        public int x, y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public override bool Equals(object obj)
        {
            // If this and obj do not refer to the same type, then they are not equal.
            if (obj.GetType() != this.GetType()) return false;

            // Return true if  x and y fields match.
            Point other = (Point)obj;
            return (this.x == other.x) && (this.y == other.y);
        }

        // Return the XOR of the x and y fields.
        public override int GetHashCode()
        {
            return x ^ y;
        }

        // Return the point's value as a string.
        public override String ToString()
        {
            return String.Format("({0}, {1})", x, y);
        }

        // Return a copy of this point object by making a simple field copy.
        public Point Copy()
        {
            return (Point)this.MemberwiseClone();
        }
    }

DateTime

获取当前系统时间
DateTime dt = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-dd"));
Console.WriteLine(dt.ToString()); //26/11/2009 AM 11:21:30

Console.WriteLine(dt.AddYears(1).ToString());    //         26/11/2010 PM 12:29:51
Console.WriteLine(dt.AddDays(1.1).ToString());    //        27/11/2009 PM 2:53:51
Console.WriteLine(dt.AddHours(1.1).ToString());    //       26/11/2009 PM 1:35:51
Console.WriteLine(dt.AddMilliseconds(1.1).ToString());    //26/11/2009 PM 12:29:51
Console.WriteLine(dt.AddMonths(1).ToString());    //        26/12/2009 PM 12:29:51
Console.WriteLine(dt.AddSeconds(1.1).ToString());    //     26/11/2009 PM 12:29:52
Console.WriteLine(dt.AddMinutes(1.1).ToString());    //     26/11/2009 PM 12:30:57
Console.WriteLine(dt.AddTicks(1000).ToString());    //      26/11/2009 PM 12:29:51

运算符重载

通过关键字operator后跟运算符的符号来定义的(个人认为最常见的运算符重载是+)

public static Box operator+ (Box b,Box c){
   Box box = new Box(); //创建一个对象用作运算之后的返回对象
   box.length = b.length + c.length; //对对象的内容进行处理
   box.breadth = b.breadth + c.breadth;
   box.height = b.height + c.height;
   return box; //返回对象
}

//使用
Box box3 = new Box();
box3 = box2+box1;

委托

使用delegate关键字声明委托

委托类似于c/c++中的函数指针

它可以保存某个方法的引用

委托特别用于实现事件的回调方法

所有的委托都派生自System.Delegate

//声明委托
delegate int NumberChanger(int n);
//声明委托的方法
static int AddNum(int p);

//创建委托
NumberChanger nc;
NumberChanger nc1 = new NumberChanger(AddNum)
//委托之间可以相加
nc += nc1;
//检测委托有多少个?

接口

interface MyInterface{
  void MethodToImplement();
}
//实现接口
class InterfaceImplement : MyInterface {

  public void MethodToImplement(){
    Console.WriteLine("MethodToImplement() called.");
  }
}

MyInterface iImp = new InterfaceImplement();
//调用接口
iImp.MethodToImplement();

反射

可以调用类型对象的方法访问其字段或者属性

using System;
using System.Reflection; //

[AttributeUsage (AttributeTargets.Class | //规定了特性被放在Class前面
AttributeTargets.Constructor | //规定了特性被放在构造函数前面
AttributeTargets.Field | //规定了特性被放在域的前面
AttributeTargets.Method | //规定了特性被放在方法前面
AttributeTargets.Property, AllowMuleiple = true)] //标记了我们的定制特性能否被重复放置在同一个实体多次

public class DebugInfo : System.Attribute {
  private int bugNo;
  private string developer;
  private string lastReview;
  public string message;

  public DeBugInfo(int bg, string dev, string d){
    this.bugNo = bg;
    this.developer = dev;
    this.lastReview = d;
  }

  public int BugNo{
    get {
      return bugNo;
    }
  }

  public string Developer {
    get { return developer;}

  }

  public string LastReview{
    get { return lastReview;}
  }

  public string Message {
    get { return message;}

    set { message = vallue; }
  }

  [DebugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
  [DebugInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]
  class Rectangle {
    protected double length;
    protected double width;
    public Rectangle(double l, double w){
      length = 1;
      width = w;
    }

    [DeBugInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")]
    public double GetArea(){
      return length * width;
    }

    [DeBugInfo(56, "Zara Ali", "19/10/2012")]
    public void Display(){
       Console.WriteLine("Length: {0}", length);
       Console.WriteLine("Width: {0}", width);
       Console.WriteLine("Area: {0}", GetArea());
    }
  }

  class ExecuteRectangle {
    static void Main(string[] args){
      Rectangle r = new Rectangle(4.5, 7.5);
      r.Display();
      Type type = typeof(Rectangle);
      // 遍历 Rectangle 类的特性
      foreach( Object attributes in type.GetCustomAttributes(false)){
        DeBugInfo dbi = (DeBugInfo)attributes;
        if(null != dbi){
          Console.WriteLine("Bug no: {0}", dbi.BugNo);
          Console.WriteLine("Developer: {0}", dbi.Developer);
          Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
          Console.WriteLine("Remarks: {0}", dbi.Message);
        }
      }

      //遍历方法特性
    foreach(MethodInfo m in type.GetMethods()){
      foreach(Attribute a in m.GetCustomAttributes(true)){
        DebugInfo dbi = (DebugInfo)a;
        if(null != dbi){
          Console.WriteLine("Bug no:")
          }
        }
      }
    }

  }
}

特性 调试备注

特性(Attribute) 是用于在运行时传递程序中各种元素行为信息的声明式标签,通过放置在所运营的元素前面用[]来描述

//标记已过时
[Obsolete("Dot't use OldMethod, user NewNothod")]

//自定义特性
public class DebugInfo : System.Attribute {
  private int bugNo;
  private string developer;
  private string lastReview;
  public string message;
  public DeBugInfo(int bg, string dev, string d){
    this.bugNo = bg;
    this.developer = dev;
    this.lastReview = d;
  }

  ....

}

多线程 同步

重写窗体事件DefWndProc接收自定义消息
发送消息
  string myText = textBox1.Text;
            SENDDATASTRUCT myData = new SENDDATASTRUCT();
            myData.lpData = myText;
            SendMessage(SendToHandle, MYMESSAGE, 100, ref myData);//发送自定义消息给句柄为SendToHandle 的窗口

参考资料:https://blog.csdn.net/weixin_34342992/article/details/94756731

控件部分

基础控件

Form 基础框架

工具栏

ToolStrip

ToolStripControlHost host = new ToolStripControlHost(panel);
toolStrip1.Items.Add(host);

FlowLayoutPanel

60437484

SplitContainer

TableLayoutPanel

NotifyIcon 任务栏显示气泡提示

//任务栏设置并显示气泡提示信息
        private void btn_SetInfo_Click(object sender, EventArgs e)
        {
            notifyIcon1.ShowBalloonTip(2000, "图标介绍", "柳岩身材一级棒!", ToolTipIcon.Info);
        }

        //鼠标移过图标时显示默认气泡提示信息
        private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
        {
            notifyIcon1.ShowBalloonTip(1000);
        }

https://www.cnblogs.com/ljhandsomeblog/p/11211484.html

FlowLayoutPanel()

var panel = new FlowLayoutPanel();
panel.FlowDirection = FlowDirection.BottomUp;
panel.Controls.Add(new Button(){ Text = ""})
panel.Controls.Add(new Button(){ Text = ""})

状态栏

菜单

MenuStrip menuStrip1 = new System.Windows.Forms.MenuStrip();
menuStrip1.SuspendLayout();
//一级菜单创建
this.菜单ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

menuStrip1.ImageScalingSize = new System.Drawing.Size(20,20);
//将一级菜单添加到menuStrip
menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[]{
  this.菜单ToolStripMenuITem,
  this.帮助ToolStripMenuItem
})
menuStrip1.Location = new System.Drawing.Point(0,0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = System.Drawing.Size(585, 28);
menuStrip1.TabIndex = 4;
menuStrip1.Text = "menuStrip1";

ToolStripMenuItem item_open;
ToolStripMenuItem item_save;
//二级菜单创建
item_open = new ToolStripMenuItem();
item_open.Name = "菜单ToolStripMenuItem"
item_open.Size = new System.Drawing.Size(53, 14);
item_open.Text = "菜单"
//将二级菜单添加到一级
菜单ToolStripMenuITem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[]{
  item_open,
  item_save
})

Button 按钮

Button btn_setting = new Button();
btn_setting.Name = "btn_setting";
btn_setting.Text = "设置";
btn_setting.Location = new System.Drawing.Point(39, 200);
btn_setting.Size = new System.Drawing.Size(495, 50);
btn_setting.TabIndex = 1;
btn_setting.UseVisualStyleBackColor = true;
btn_setting.Click += new System.EventHandler(this.Click_setting);

Label 文字标签

Label label = new Label();
label.AutoSize = true;
label.Locationo = new System.Drawing.Point(39, 139);
label.Size = new System.Drawing.Size(54,20);
label.TabIndex = 0;
label.Text = "密码";
this.Controls.Add(this.label);

TextBox 输入框

TextBox textBox = new TextBox();
textBox.Name = "textBox_pass";
textBox.PasswordChar = '*';
textBox.Size = new System.Drawing.Size(495, 27);
textBox.TabIndex = 1;

NumericUpDown 数字输入框

MumericUpDown numericUpDown1 = new NumericUpDown();
numericUpDown1.Location = new System.Drawing.Point(39,32);
numericUpDown1.Maximum = new decimal(new int[]{1000,0,0,0});
numericUpDown1.Name = "numericUpDown1";
numericUpDown1.Size = new System.Drawing.Size(150, 27);
numericUpDown1.TabIndex = 5;
numericUpDown1.Value = new decimal(new int[]{100, 0, 0, 0});

this.Controls.Add(this.numericUpDown1);

ListBox

ListBox listBox = new ListBox();
listBox.Items.Add("第一行");
//检查是否添加过
if(listBox.Items.Contains("第一行")){
  MessageBox.Show("集合成员已添加过!");
}
if(listBox.SelectedItems.Count > 0) {
  listBox.SelectedItem.ToString(); //获取选中项的值
}

ListView

//设置ListView标题栏
            this.listView.Columns.Add("车型", 120, HorizontalAlignment.Left); //
            this.listView.Columns.Add("号牌种类", 120, HorizontalAlignment.Left); //
            this.listView.Columns.Add("车牌号", 120, HorizontalAlignment.Left); //
            this.listView.Columns.Add("燃料种类", 120, HorizontalAlignment.Left); //
//设置显示模式
listView.View = View.Details;

//设置ImageList
ImageList imgList = new ImageList();
                    imgList.ImageSize = new Size(1, 20);// 设置行高 20 //分别是宽和高  

                    listView.SmallImageList = imgList; //这里设置listView的SmallImageList ,用imgList将其撑大  

//添加一个选项
                        var item = model.data[i];
                        ListViewItem lvi = new ListViewItem();

                        lvi.ImageIndex = i;     //通过与imageList绑定,显示imageList中第i项图标  

                        lvi.Text = item.CLLX;

                        lvi.SubItems.Add(item.HPZL);

                        lvi.SubItems.Add(item.HPHM);
                        lvi.SubItems.Add(item.RLZL);

                        listView.Items.Add(lvi);

https://www.cnblogs.com/ljhandsomeblog/p/11169024.html

ComboBox 下拉选择框

ComboBox comboBox = new ComboBox();
comboBox.Text = "请选择项目";
comboBox.SelectedIndex = 0;
comboBox.SelectedItem.ToString();
comboBox.Items.Add("第一项");
comboBox.Items.Add("第二项");

PictureBox

#region 控件文件拖拽获取事件
        private void img_data_DragDrop(object sender, DragEventArgs e)
        {
            //获取所有拖拽文件路劲
            System.Array array = (System.Array)e.Data.GetData(DataFormats.FileDrop);
            int index = array.Length;
            for (int i = 0; i < index; i++)
            {
                listPath.Add(array.GetValue(i).ToString());
            }

            if (listPath.Count > 0)
            {
                BindImgData(listPath[0], 1);
            }
        }

        private void img_data_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        /// <summary>
        /// 图片绑定
        /// </summary>
        /// <param name="path"></param>
        /// <param name="page"></param>
        private void BindImgData(string path, int page)
        {
            try
            {

                sbtn_Prev.Enabled = true;
                sbtn_Next.Enabled = true;

                if (page == 1) { sbtn_Prev.Enabled = false; }
                if (page == listPath.Count) { sbtn_Next.Enabled = false; }

                fileExtension = Path.GetExtension(path).ToLower();
                fileName = Path.GetFileName(path);
                sbtn_Pages.Text = page + "/" + listPath.Count;

                if (path.Contains("http://") || path.Contains("https://"))
                {
                    picObj = Bitmap.FromStream(new System.IO.MemoryStream((new System.Net.WebClient()).DownloadData(path)));
                }
                else
                {
                    picObj = Image.FromFile(path);
                }
            }
            catch (Exception ex)
            {
                picObj = null;
                sbtn_Pages.Text = "0";

                //string strNo = Helpers.UtilityHelper.GetSerialNumber();
                //Helpers.LogHelper.ErrorMsgRecord(this.Name, ex, strNo);
                //Helpers.LoadingHelper.CloseForm();
                //MessageBox.Show("图片对象或路径异常,显示失败!\r\n" + ex.Message + strNo, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            img_data.Image = picObj;

        }

        #endregion       

        #region 鼠标移动图片
        bool dragFlag = false;
        int mouseX = 0, mouseY = 0;
        private void img_data_MouseDown(object sender, MouseEventArgs e)
        {
            mouseX = e.X;
            mouseY = e.Y;
            dragFlag = true;
        }

        private void img_data_MouseUp(object sender, MouseEventArgs e)
        {
            dragFlag = false;
        }

        private void img_data_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragFlag)
            {
                img_data.Location = new Point(img_data.Location.X - (mouseX - e.X), img_data.Location.Y - (mouseY - e.Y));
            }
        }
        #endregion

https://www.cnblogs.com/ljhandsomeblog/p/11213094.html

CheckedListBox

//添加列表项,勾选指定条件的项
        private void btn_AddItem_Click(object sender, EventArgs e)
        {
            string value = txt_ItemText.Text;
            if (!string.IsNullOrWhiteSpace(value))
            {
                if (!this.checkedListBox1.Items.Contains(value))
                {
                    bool isRn = value.Contains("帅") ? true : false;
                    this.checkedListBox1.Items.Add(value, isRn);

                    checkedListBox1_SelectedIndexChanged(null, null);
                }

            }
        }
        //获取勾选项并展示
        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0, counti = checkedListBox1.Items.Count; i < counti; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    sb.AppendFormat("{0},", checkedListBox1.GetItemText(checkedListBox1.Items[i]));
                }
            }
            this.txt_Result.Text = sb.ToString().Trim(',');
        }

https://www.cnblogs.com/ljhandsomeblog/p/11127236.html

Timer

  Enabled - 控件是否启用

  Interval - 事件的频率,多长时间触发一次时间(李献策lx
  

ProgressBar

ProgressBar progressBar = new ProgressBar();
progressBar.Name = "ProgressBar";
progressBar.Value = 40;
progressBar.Minimum = 0;
progressBar.Maximum = 100;

DataGridView

相关资料:http://m.biancheng.net/view/3040.html

操作DataTable:https://blog.csdn.net/panliuwen/article/details/47710421

TabControl

TabControl tabControl = new TabControl;
page1 = new TabPage();
page2 = new TabPage();
tabControl.Location

page1.SuspendLayout();
page2.SuspendLayout();

tabControl.SuspendLayout();

DataGridView

http://c.biancheng.net/view/3040.html

TreeView

private void btn_LoadData_Click(object sender, EventArgs e)
        {
            //设置树形组件的基础属性
            treeView1.CheckBoxes = true;
            treeView1.FullRowSelect = true;
            treeView1.Indent = 20;
            treeView1.ItemHeight = 20;
            treeView1.LabelEdit = false;
            treeView1.Scrollable = true;
            treeView1.ShowPlusMinus = true;
            treeView1.ShowRootLines = true;

            //需要加载树形的源数据
            string[] strData = { "1;内地;柳岩",
                                 "2;内地;杨幂",
                                 "3;欧美;卡戴珊",
                                 "4;日韩;李成敏",
                                 "5;日韩;宇都宫紫苑"};

            //解析到DataTable数据集
            DataTable dtData = new DataTable();
            dtData.Columns.Add("ID");
            dtData.Columns.Add("GROUP");
            dtData.Columns.Add("NAME");

            foreach (string item in strData)
            {
                string[] values = item.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (values.Length == 3)
                {
                    DataRow row = dtData.NewRow();
                    row["ID"] = values[0];
                    row["GROUP"] = values[1];
                    row["NAME"] = values[2];
                    dtData.Rows.Add(row);
                }
            }

            TreeNode tn = new TreeNode();
            tn.Name = "全部";
            tn.Text = "全部";

            //将数据集加载到树形控件当中
            foreach (DataRow row in dtData.Rows)
            {
                string strValue = row["GROUP"].ToString();
                if (tn.Nodes.Count > 0)
                {
                    if (!tn.Nodes.ContainsKey(strValue))
                    {
                        BindTreeData(tn, dtData, strValue);
                    }
                }
                else
                {
                    BindTreeData(tn, dtData, strValue);
                }
            }

            treeView1.Nodes.Add(tn);
            treeView1.ExpandAll();
        }

        private void BindTreeData(TreeNode tn, DataTable dtData, string strValue)
        {
            TreeNode tn1 = new TreeNode();
            tn1.Name = strValue;
            tn1.Text = strValue;
            tn.Nodes.Add(tn1);

            DataRow[] rows = dtData.Select(string.Format("GROUP='{0}'", strValue));
            if (rows.Length > 0)
            {
                foreach (DataRow dr in rows)
                {
                    TreeNode tn2 = new TreeNode();
                    tn2.Name = dr["GROUP"].ToString();
                    tn2.Text = dr["NAME"].ToString();
                    tn1.Nodes.Add(tn2);
                }
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            //鼠标勾选树节点时需要使树节点为选中状态,反之忽略
            if (isMouseClick)
            {
                treeView1.SelectedNode = e.Node;
            }

            //获取勾选项名称
            StringBuilder sb = new StringBuilder();
            GetTreeNodesCheckName(sb, treeView1.Nodes);
            txt_CheckValue.Text = sb.ToString().Trim(';');
        }

        private void GetTreeNodesCheckName(StringBuilder sb, TreeNodeCollection tnc)
        {
            foreach (TreeNode item in tnc)
            {
                if (item.Checked) { sb.AppendFormat("{0};", item.Text); }

                GetTreeNodesCheckName(sb, item.Nodes);
            }
        }

        bool isMouseClick = true;
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //选中或勾选树节点时触发子树节点或父树节点的逻辑操作
            isMouseClick = false;

            SetCheckedChildNodes(e.Node, e.Node.Checked);

            SetCheckedParentNodes(e.Node, e.Node.Checked);

            isMouseClick = true;
        }

        //树节点的父树节点逻辑操作
        private static void SetCheckedParentNodes(TreeNode tn, bool CheckState)
        {
            if (tn.Parent != null)
            {
                //当选中树节点勾选后同级所有树节点都勾选时,父树节点为勾选状态;
                //当选中树节点中的同级树节点其中有一个树节点未勾选则父树节点为未勾选状态;
                bool b = false;
                for (int i = 0; i < tn.Parent.Nodes.Count; i++)
                {
                    bool state = tn.Parent.Nodes[i].Checked;
                    if (!state.Equals(CheckState))
                    {
                        b = !b;
                        break;
                    }
                }
                tn.Parent.Checked = b ? (Boolean)false : CheckState;

                SetCheckedParentNodes(tn.Parent, CheckState);
            }
        }

        //树节点的子树节点逻辑操作
        private static void SetCheckedChildNodes(TreeNode tn, bool CheckState)
        {
            if (tn.Nodes.Count > 0)
            {
                //当前树节点状态变更,子树节点同步状态
                foreach (TreeNode tn1 in tn.Nodes)
                {
                    tn1.Checked = CheckState;

                    SetCheckedChildNodes(tn1, CheckState);
                }
            }
        }

https://www.cnblogs.com/ljhandsomeblog/p/11225879.html

DateTimePicker

CustomFormat:当Format属性设置为自定义类型时可自定义控件时间的显示格式;

Enabled:指示是否启用该控件,true为启用状态可编辑,false为禁用状态不可编辑;

MaxDate:设置控件可选择或输入的最大日期;

MinDate:设置控件可选择或输入的最小日期;

Name:指示代码中用来标识该对象的名称;

ShowUpDown:是否使用下拉日历修改日期,false为下拉日历模式,true为区域数字增减模式;

Text:与控件关联的文本,显示给用户看的内容说明;

ValueChanged事件:控件值更改时发生;

https://www.cnblogs.com/ljhandsomeblog/p/11128338.html

MonthCalender 日期控件,可选择范围

        //获取控件选中日期
        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            txt_date.Text = string.Format("{0} 至 {1}", monthCalendar1.SelectionStart, monthCalendar1.SelectionEnd);

        }

MaskedTextBox 掩码文本控件,使用掩码来区分用户输入文本是否正确。

ComboBox 下拉文本框

//加载下拉项
        private void btn_LoadItem_Click(object sender, EventArgs e)
        {
            string[] strGirls = lbl_Girls.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (strGirls.Length > 0)
            {
                this.comboBox1.Items.Clear();
                this.comboBox1.Items.Add("全部");
                this.comboBox1.Items.AddRange(strGirls);
                this.comboBox1.SelectedIndex = 0;
                this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            }
        }

https://www.cnblogs.com/ljhandsomeblog/p/11128015.html

Panel

RichTextBox

DetectUrls:指示是否自动将URL的格式设置为链接;

EnableAutoDragDrop:是否启用文本、图片和其他数据的拖放操作;

BorderStyle:指示编辑控件是否应带有边框或边框类型;

Lines:多行编辑中的文本行,作为字符串值的数组;

MaxLength:指定可以在编辑控件中输入的最大字符数;

Multiline:控制编辑控件的文本是否能够跨越多行;

ScrollBars:定义控件滚动条的行为;

WordWrap:指示多行编辑控件是否自动换行;

Enabled:指示是否启用该控件,true为启用状态用户可编辑,false为禁用状态用户不可编辑;

Name:指示代码中用来标识该对象的名称;

Text:获取或设置多格式文本框中的文本;

Rtf:获取或设置控件文本,包括所有RTF格式代码;

https://www.cnblogs.com/ljhandsomeblog/p/11214496.html

自定义控件 及 定义方法

CtrlLib.GlassButton

CtrlLib.Pager

权限系统

获取系统管理员权限

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

https://www.cnblogs.com/fj99/p/4239690.html?ivk_sa=1024320u

修改注册表

设置程序开机自动运行

/// <summary>  
        /// 在注册表中添加、删除开机自启动键值  
        /// </summary>  
        public static int SetAutoBootStatu(bool isAutoBoot)
        {
            try
            {
                //RegistryKey rk = Registry.LocalMachine;
                //RegistryKey rk2 = rk.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
                //rk2.SetValue("MyExec", execPath);
                string execPath = Application.ExecutablePath;
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey rk2 = rk.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
                if (isAutoBoot)
                {
                    rk2.SetValue("MyExec", execPath);
                    Console.WriteLine(string.Format("[注册表操作]添加注册表键值:path = {0}, key = {1}, value = {2} 成功", rk2.Name, "TuniuAutoboot", execPath));
                }
                else
                {
                    rk2.DeleteValue("MyExec", false);
                    Console.WriteLine(string.Format("[注册表操作]删除注册表键值:path = {0}, key = {1} 成功", rk2.Name, "TuniuAutoboot"));
                }
                rk2.Close();
                rk.Close();
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("[注册表操作]向注册表写开机启动信息失败, Exception: {0}", ex.Message));
                return -1;
            }
        }

获取 文档/桌面/音乐/下载 目录

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //文档目录
string musicPath = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); //音乐目录
string tempPath = Environment.GetFolderPath(Environment.SpecialFolder.Templates); //temp目录
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //桌面目录

获取应用程序所在路径
System.AppDomain.CurrentDomain.BaseDirectory
或者
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase

代码部分

设置窗口图标

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

设置窗口名字

this.Text = "标题";

应用全屏,隐藏状态栏

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

2、实现窗体内某控件的全屏显示

方法:例如要将richtextbox控件全屏显示,操作如下(this是当前窗体)

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState=FormWindowState.Maximized;
Rectangle ret = Screen.GetWorkingArea(this);

this.richTextBox2.ClientSize = new Size(ret.Width, ret.Height);
this.richTextBox2.Dock = DockStyle.Fill;
this.richTextBox2.BringToFront();

3、退出全屏,恢复原貌

方法:前提是先定义一个类成员变量,用于保存要全屏控件的原始尺寸(Size),然后在构造函数内将其初始化为控件原始尺寸

在退出全屏方法内,操作如下

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Normal;
this.richTextBox2.ClientSize = primarySize;//primarySize即是控件的原始尺寸
this.richTextBox2.Dock = DockStyle.None;

加载Gif图片

按钮设置点击时间

弹出对话框

 //消息框中需要显示哪些按钮,此处显示“确定”和“取消”

 MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

 //"确定要退出吗?"是对话框的显示信息,"退出系统"是对话框的标题

 //默认情况下,如MessageBox.Show("确定要退出吗?")只显示一个“确定”按钮。
 DialogResult dr = MessageBox.Show("确定要退出吗?", "退出系统", messButton);

 if (dr == DialogResult.OK)//如果点击“确定”按钮

 {

   ……

 }

else//如果点击“取消”按钮

{

  ……

}

MessageBoxButtons指定若干常数,用以定义MessageBox上将显示哪些按钮(来自MSDN)

MessageBoxButtons成员:

  成员名称                                  说明

AbortRetryIgnore 消息框包含“中止”、“重试”和“忽略”按钮。

OK                                  消息框包含“确定”按钮。(默认) 
OKCancel                      消息框包含“确定”和“取消”按钮。(上例所示) 
RetryCancel      消息框包含“重试”和“取消”按钮。

YesNo                       消息框包含“是”和“否”按钮。 
YesNoCancel          消息框包含“是”、“否”和“取消”按钮

弹出带输入框的对话框

修改窗口固定宽高,不允许修改

窗口可自由设置宽高

设置按钮样式(背景色,文字颜色,字体,字体大小)

加载本地图片

fileStream = new FileStream("图片路径", FileMode.Open, FileAccess.Read);
pictureBox1.Image = Image.FromStream(fileStream);

加载resouce图片

pictureBox1.Image = Properties.Resources.headimg_dl_android;

加载网络图片

this.QRCodePictureBox.LoadAsync(“https://injiajia.com/qrcode.png”);

播放声音

//System.Media.SystemSounds.Asterisk.Play(); //系统提示音
            //System.Media.SystemSounds.Beep.Play(); //系统提示音
            //System.Media.SystemSounds.Exclamation.Play(); 
            System.Media.SystemSounds.Hand.Play(); //错误提示
            //System.Media.SystemSounds.Question.Play(); //没有听到声音

2.使用System.Media.SoundPlayer播放wav
System.Media.SoundPlayer sp = new SoundPlayer(); 
sp.SoundLocation = @"D:/10sec.wav"; 
sp.PlayLooping();

3.使用MCI Command String多媒体设备程序接口播放mp3,avi等
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace MyWenxinTool
{
    public class musicplay
    {

        public static uint SND_ASYNC = 0x0001;
        public static uint SND_FILENAME = 0x00020000;
        [DllImport("winmm.dll")]
        public static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, uint hWndCallback);

        public static void PlayNmusinc(string path)
        {
            mciSendString(@"close temp_alias", null, 0, 0);
            mciSendString(@"open """+path+@""" alias temp_alias", null, 0, 0);
            mciSendString("play temp_alias repeat", null, 0, 0);
        }

        /// <summary>
        /// 播放音乐文件(重复)
        /// </summary>
        /// <param name="p_FileName">音乐文件名称</param>
        public static void PlayMusic_Repeat(string p_FileName)
        {
            try
            {
                mciSendString(@"close temp_music", " ", 0, 0);
                mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);
                mciSendString(@"play temp_music repeat", " ", 0, 0);
            }
            catch
            { }
        }

        /// <summary>
        /// 播放音乐文件
        /// </summary>
        /// <param name="p_FileName">音乐文件名称</param>
        public static void PlayMusic(string p_FileName)
        {
            try
            {
                mciSendString(@"close temp_music", " ", 0, 0);
                //mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);
                mciSendString(@"open """ + p_FileName + @""" alias temp_music", null, 0, 0);
                mciSendString(@"play temp_music", " ", 0, 0);
            }
            catch
            { }
        }

        /// <summary>
        /// 停止当前音乐播放
        /// </summary>
        /// <param name="p_FileName">音乐文件名称</param>
        public static void StopMusic(string p_FileName)
        {
            try
            {
                mciSendString(@"close " + p_FileName, " ", 0, 0);
            }
            catch { }
        }

    }

}

保存ini数据

 public class ReadIni
    {
        // 声明INI文件的写操作函数 WritePrivateProfileString()

        [System.Runtime.InteropServices.DllImport("kernel32")]

        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 声明INI文件的读操作函数 GetPrivateProfileString()

        [System.Runtime.InteropServices.DllImport("kernel32")]

        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        private string sPath = null;
        public ReadIni(string path)
        {
            this.sPath = path;
        }

        public void Writue(string section, string key, string value)
        {

            // section=配置节,key=键名,value=键值,path=路径

            WritePrivateProfileString(section, key, value, sPath);

        }
        public string ReadValue(string section, string key)
        {

            // 每次从ini中读取多少字节

            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节,key=键名,temp=上面,path=路径

            GetPrivateProfileString(section, key, "", temp, 255, sPath);

            return temp.ToString();

        }

    }

生成json数据

JsonObject json = new JsonObject();
 json["key"] = objDE.Value;
 Console.WriteLine(json.ToString());

保存xml数据

json格式化

文件读取和写入

class FileUtil
    {
        //C#追加文件
        public static void append(string path, string text)
        {
            StreamWriter sw = File.AppendText(path);
            sw.WriteLine(text);
            sw.Flush();
            sw.Close();
        }

        //C#拷贝文件
        public static void copy(string oldPath, string newPath)
        {
            string OrignFile, NewFile;
            OrignFile = oldPath;
            NewFile = newPath;
            File.Copy(OrignFile, NewFile, true);
        }

        //C#删除文件
        public static void delete(string path)
        {
            string delFile = path;
            File.Delete(delFile);
        }

        //C#移动文件
        public static void move(string oldPath, string newPath)
        {
            string OrignFile, NewFile;
            OrignFile = oldPath;
            NewFile = newPath;
            File.Move(OrignFile, NewFile);
        }

        //C#创建目录
        public static void mkdir(string path)
        {
            // 创建目录c:\sixAge
            DirectoryInfo d = Directory.CreateDirectory(path);
            // d1指向c:\sixAge\sixAge1
            //DirectoryInfo d1 = d.CreateSubdirectory("sixAge1");
            // d2指向c:\sixAge\sixAge1\sixAge1_1
            //DirectoryInfo d2 = d1.CreateSubdirectory("sixAge1_1");
            // 将当前目录设为c:\sixAge
            //Directory.SetCurrentDirectory("c:\\sixAge");
            //创建目录c:\sixAge\sixAge2
            //Directory.CreateDirectory("sixAge2");
            // 创建目录c:\sixAge\sixAge2\sixAge2_1
            //Directory.CreateDirectory("sixAge2\\sixAge2_1");
        }

        //递归删除文件夹及文件
        public void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir)) //如果存在这个文件夹删除之
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                        File.Delete(d); //直接删除其中的文件
                    else
                        DeleteFolder(d); //递归删除子文件夹
                }
                Directory.Delete(dir); //删除已空文件夹
                Console.WriteLine(dir + " 文件夹删除成功");
            }
            else
                Console.WriteLine(dir + " 该文件夹不存在"); //如果文件夹不存在则提示
        }

        // ======================================================
        // 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面
        // 如果目标文件夹为只读属性就会报错。
        // April 18April2005 In STU
        // ======================================================
        public static void CopyDir(string srcPath, string aimPath)
        {

            try
            {

                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 判断目标目录是否存在如果不存在则新建之
                if (!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
                // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
                // string[] fileList = Directory.GetFiles(srcPath);
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                // 遍历所有的文件和目录
                foreach (string file in fileList)
                {

                    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (Directory.Exists(file))
                        CopyDir(file, aimPath + Path.GetFileName(file));
                    // 否则直接Copy文件
                    else
                        File.Copy(file, aimPath + Path.GetFileName(file), true);
                }
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }
        }

        // ======================================================
        // 实现一个静态方法将指定文件夹下面的所有内容Detele
        // 测试的时候要小心操作,删除之后无法恢复。
        // April 18April2005 In STU
        // ======================================================
        public static void DeleteDir(string aimPath)
        {

            try
            {

                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                // 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
                // string[] fileList = Directory.GetFiles(aimPath);
                string[] fileList = Directory.GetFileSystemEntries(aimPath);
                // 遍历所有的文件和目录
                foreach (string file in fileList)
                {

                    // 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件
                    if (Directory.Exists(file))
                    {

                        DeleteDir(aimPath + Path.GetFileName(file));
                    }
                    // 否则直接Delete文件
                    else
                    {

                        File.Delete(aimPath + Path.GetFileName(file));
                    }
                }
                //删除文件夹
                System.IO.Directory.Delete(aimPath, true);
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }
        }
    }

//文本文件读取
 FileStream fop = File.OpenRead(str);
                                 byte[] arr = new byte[1000];
                                 while (fop.Read(arr, 0, arr.Length) > 0)    ///这个循环会将文件所有的内容都读取出来
                                  {
                                  ClientSocket[1].Send(arr, 0, arr.Length,0);
                                  }
                                 fop.Close();

//文本文件写入
 string path = "C:\\test.txt";

                            if (File.Exists(path))      ///如果文件存在,那么删除文件 

                                     File.Delete(path);

                            FileStream fs = File.Open(path, FileMode.Create);         ///这里第二个表示如果文件不存在,则重新建立一个新的
                            ///FileStream fs = File.Open(path, FileMode.Append);  ///如果要追加内容时用这个

                            fs.Write(bs, 0, bs.Length);                     ///这里的bs是一个数组byte[]

                            fs.Close();

调用文件选择器选择文件 / 文件夹

private void btnSelectPath_Click(object sender, EventArgs e) //弹出一个选择目录的对话框  
 {  
    FolderBrowserDialog path = new FolderBrowserDialog();  
    path.ShowDialog();  
    this.txtPath.Text = path.SelectedPath;  
 }  
private void btnSelectFile_Click(object sender, EventArgs e) //弹出一个选择文件的对话框  
 {  
    OpenFileDialog file = new OpenFileDialog();  
    file.ShowDialog();  
    this.txtFile.Text = file.SafeFileName;  
 }  

配置应用名称、版本号、

AssemblyInfo.cs 里 [assembly: AssemblyVersion("1.2.0.0")]

获取应用名称、版本号

Assembly.GetExecutingAssembly().GetName().Version.ToString();//获取当前应用程序版本

Windows应用签名

WIndows应用加固

Windows应用压缩

打开文件对话框

打开文件夹对话框

动画

https://www.jb51.net/article/189920.htm

平移动画

缩放动画

透明度渐变动画

高级

获取当前类名、名称空间、方法名

1.(new StackTrace()).GetFrame(1) // 0为本身的方法;1为调用方法
2.(new StackTrace()).GetFrame(1).GetMethod().Name; // 方法名
3.(new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name; // 类名

给类添加扩展方法

使用sqlite数据库

https://www.cnblogs.com/springsnow/p/13072915.html

使用sql server数据库

https://www.cnblogs.com/wangxueliang/p/9346470.html

使用mysql数据库

使用oracle数据库

实现socket server websocket

“Fleck”包 websocket-sharp包 WebSocket4Net

https://blog.csdn.net/qq_35955916/article/details/86529647

文件读写

https://www.cnblogs.com/kafeibuku/p/5320350.html

加载http数据

下载文件

实现http服务器

串口通信

 public class Comm   
     {
        public delegate void EventHandle(byte[] readBuffer);
        public event EventHandle DataReceived;

        public SerialPort serialPort;
        Thread thread;
        volatile bool _keepReading;

        public Comm()
        {
            serialPort = new SerialPort();
            thread = null;
            _keepReading = false;
        }

        public bool IsOpen
        {
            get 
            {
                return serialPort.IsOpen;
            }
        }        

        private void StartReading()
        {
            if (!_keepReading)
            {
                _keepReading = true;
                thread = new Thread(new ThreadStart(ReadPort));
                thread.Start();
            }
        }

        private void StopReading()
        {
            if (_keepReading)
            {
                _keepReading = false;
                thread.Join();
                thread = null;                
            }
        }

        private void ReadPort()
        {
            while (_keepReading)
            {
                if (serialPort.IsOpen)
                {
                    int count = serialPort.BytesToRead;
                    if (count > 0)
                    {
                        byte[] readBuffer = new byte[count];
                        try
                        {
                            Application.DoEvents();
                            serialPort.Read(readBuffer, 0, count);
                            if(DataReceived != null)
                                DataReceived(readBuffer);
                            Thread.Sleep(100);
                        }
                        catch (TimeoutException)
                        {
                        }
                    }
                }
            }
        }

        public void Open()
        {
            Close();            
            serialPort.Open();
            if (serialPort.IsOpen)
            {
                StartReading();
            }
            else
            {
                MessageBox.Show("串口打开失败!");
            }
        }

        public void Close()
        {
            StopReading();
            serialPort.Close();
        }

        public void WritePort(byte[] send, int offSet, int count)
        {
            if (IsOpen)
            {
                serialPort.Write(send, offSet, count);
            }
        }
    }

注册串口

 Comm    comm = new Comm();
            ConfigClass config = new ConfigClass();
            comm.serialPort.PortName = config.ReadConfig("SendHealCard");
            //波特率
            comm.serialPort.BaudRate = 9600;
            //数据位
            comm.serialPort.DataBits = 8;
            //两个停止位
            comm.serialPort.StopBits = System.IO.Ports.StopBits.One;
            //无奇偶校验位
            comm.serialPort.Parity = System.IO.Ports.Parity.None;
            comm.serialPort.ReadTimeout = 100;
    comm.serialPort.WriteTimeout = -1; 

      comm.Open();
            if (comm.IsOpen)
            {
                comm.DataReceived += new Comm.EventHandle(comm_DataReceived);
            }
//https://www.cnblogs.com/binfire/archive/2011/10/08/2201973.html

http://www.360doc.com/content/13/0829/09/7531335_310657574.shtml

解析json

方法一:
  model = JsonConvert.DeserializeObject<JSONModel>(reString);
方法二:(.NET Framework4.0不支持)
  JsonObject jsonObject = new JsonObject(reString,true);

解析xml

方法一:
JsonObject jsonobj = new JsonObject();
jsonobj.LoadXml("<string name=\"haha\" value=\"\"/>");
Console.WriteLine(jsonobj.ToString());
方法二:
   XmlDocument xmlDoc = new XmlDocument();  
    xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><HH><CODE>11</CODE><NAME>kkkk</NAME></HH>");  
    XmlNode rootNode = xmlDoc.SelectSingleNode("HH");  
    foreach (XmlNode xxNode in rootNode.ChildNodes)  
    {  
        string dsf = xxNode.InnerText;  
        string sdf = xxNode.Name;  
    } 

https://www.cnblogs.com/hnsongbiao/p/5636076.html

反射解析json

生成excel

https://www.cnblogs.com/sdflysha/archive/2019/08/26/20190824-dotnet-excel-compare.html

生成doc

https://blog.csdn.net/a13407142317/article/details/104210356s

生成pdf

内嵌视频播放器vlc

 VlcPlayerBase player;
   string pluginPath = Environment.CurrentDirectory + "\\plugins\\";  //插件目录
            player = new VlcPlayerBase(pluginPath);
            player.SetRenderWindow((int)panel1.Handle);//panel

内嵌网页浏览器

WebBrowser

识别二维码

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
/// <summary>
        /// 读取图片文件,识别二维码
        /// </summary>
        /// <param name="filePath">图片文件路劲</param>
        /// <returns>识别结果字符串</returns>
        public static string CodeDecoder(string filePath)
        {
            string decoderStr;
            try
            {
                if (!System.IO.File.Exists(filePath))//判断有没有需要读取的主文件夹,如果不存在,终止
                    return null;

                Bitmap bitMap = new Bitmap(Image.FromFile(filePath));//实例化位图对象,把文件实例化为带有颜色信息的位图对象

                QRCodeDecoder decoder = new QRCodeDecoder();//实例化QRCodeDecoder

                //通过.decoder方法把颜色信息转换成字符串信息
                decoderStr = decoder.decode(new QRCodeBitmapImage(bitMap), System.Text.Encoding.UTF8);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            Console.WriteLine(decoderStr);
            return decoderStr;//返回字符串信息
        }

识别条形码

需要 libzbar.dll libzbar-cli.dll libiconv-2.dll

 /// <summary>
        /// 条码识别
        /// </summary>
        private void ScanBarCode(string fileName)
        {
            DateTime now = DateTime.Now;
            Image primaryImage = Image.FromFile(fileName);

            Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage);
            using (ZBar.ImageScanner scanner = new ZBar.ImageScanner())
            {
                scanner.SetConfiguration(ZBar.SymbolType.None, ZBar.Config.Enable, 0);
                scanner.SetConfiguration(ZBar.SymbolType.CODE39, ZBar.Config.Enable, 1);
                scanner.SetConfiguration(ZBar.SymbolType.CODE128, ZBar.Config.Enable, 1);

                List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
                symbols = scanner.Scan((Image)pImg);

                if (symbols != null && symbols.Count > 0)
                {
                    string result = string.Empty;
                    symbols.ForEach(s => result += "条码内容:" + s.Data + " 条码质量:" + s.Quality + Environment.NewLine);
                    MessageBox.Show(result);
                }
            }
        }

        /// <summary>
        /// 处理图片灰度
        /// </summary>
        /// <param name="original"></param>
        /// <returns></returns>
        public static Bitmap MakeGrayscale3(Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            //get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);

            //create the grayscale ColorMatrix
            System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
               new float[][] 
              {
                 new float[] {.3f, .3f, .3f, 0, 0},
                 new float[] {.59f, .59f, .59f, 0, 0},
                 new float[] {.11f, .11f, .11f, 0, 0},
                 new float[] {0, 0, 0, 1, 0},
                 new float[] {0, 0, 0, 0, 1}
              });

            //create some image attributes
            ImageAttributes attributes = new ImageAttributes();

            //set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);

            //draw the original image on the new image
            //using the grayscale color matrix
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            //dispose the Graphics object
            g.Dispose();
            return newBitmap;
        }

播放视频直播流

视频边下边播

string char bytes互相转换

//传gbk编码
byte[] buffer = Encoding.GetEncoding("GBK").GetBytes(str)
//转utf8编码
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(str)

//string转byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

//byte[]转string:
string str = System.Text.Encoding.Default.GetString ( byteArray );

NetWorkStream类的主要属性

 NetworkStream netStream = new NetworkStream(mesock);
 byte[] nyByte = new Byte[1024];
 // Read() :
 netStream.Read(myByte, 0, myByte.Length);
 // Write():
 netStream.Write(myByte, 0, myByte.Length);
 netStream.Flush();

画圆 画线

Graphics g = this.CreateGraphics();
g.DrawLine(new Pen(Color.Black,10.0f),new Point(10,10),new Point(100,10));
g.DrawRectangle(new Pen(Color.Black, 1.0f), new Rectangle(0, 0, 30, 30));
g.DrawEllipse(new Pen(Color.Black, 1.0f), new Rectangle(0, 0, 30, 30));

使用VLC播放器

https://blog.csdn.net/exalgentle/article/details/80303955

https://www.cnblogs.com/tiandi/p/6252580.html

第三方UI库

WPF设置主题:

<!-- 只需要加下面一句 -->
        <ResourceDictionary Source="J:\vs\wpfthemes主题\WPF.Themes\BubbleCreme\Theme.xaml"/>

自定义的UI组件库:https://www.cnblogs.com/anding/p/4715440.html

https://github.com/kwwwvagaa/NetWinformControl/blob/master/README_CN.md

https://gitee.com/yhuse/SunnyUI#note_4015807

使用Material Design : https://blog.csdn.net/YouyoMei/article/details/99816982

UI库合集:https://www.zhihu.com/question/311160143/answer/1340958261

自定义Win Form控件:http://www.hzhcontrols.com/doc-59.html

WinCE

C#调用C语言dll库

https://blog.csdn.net/YouyoMei/article/details/103535978

变量对应关系说明:https://docs.microsoft.com/zh-cn/dotnet/framework/interop/marshaling-data-with-platform-invoke

连接mysql / SQL Server

MySQL:https://forums.mysql.com/read.php?38,67281,67917SQL

SqlSuger:https://www.cnblogs.com/sunkaixuan/archive/2015/07/22/4649904.html

使用sqlsuger进行增删改查

//添加一条数据
Student s = new Student()
                {
                    name = "张" + new Random().Next(1, int.MaxValue)
                };

                db.Insert(s); //插入一条记录 (有主键也好,没主键也好,有自增列也好都可以插进去)
                DAL.SqlDBClient.DbClient.Insertable<Insp_Task>(task).ExecuteCommand();
//删除一条数据
db.Delete<School>(it => it.id > 100);
//update
db.Update(new School { id = id, name = "蓝翔2" });
db.Update<School>(new School { id = id, name = "蓝翔2" }, it => it.id == id);

//查询一条数据
var single = db.Queryable<Student>().Single(c => c.id == 1);
var singleOrDefault = db.Queryable<Student>().SingleOrDefault(c => c.id == 11111111);
//查询列表
cmbjcxm.DataSource = HBJCFF.Select(s => new { Text = s, Value = s }).ToList();
var list7 = db.Queryable<Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select("sex,count(*) Count").ToDynamic();
List<School> dataPageList = db.Sqlable()
                    .From("school", "s")
                    .Join("student", "st", "st.id", "s.id", JoinType.INNER)
                    .Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
                    .Where("s.id>100 and s.id<100")
                    .SelectToPageList<School>("st.*", "s.id", 1, 10);
//查询条数
var count = db.Queryable<Student>().Where(c => c.id > 10).Count();

//使用like查询
 conval = "三";
 var like = db.Queryable<Student>().Where(c => c.name.Contains(conval)).ToList();

SQL Server数据库还原

sqlcmd -S . -U aa -P ***** -d H_2019 -i J:\Documents\9.sql

SQL Server数据库常用操作

新增数据
insert into 表名 (列名,列名) values (值,值)

查询数据
select sid from student where sid=2 group by sid having sid=1 order by desc

查询单条数据

更新数据
update 数据表 set 字段名=字段值 where id = 12;

删除一条数据
delete from 数据表 where id = 12;

删除集合
delete from 数据表 where id < 12;

创建表
create table student (sname  char(20),sid  int)

删除表
drop table 表名;

在表中插入列
  use TABLE_NAME;
  ALTER TABLE dbo.Insp_Maintenance ADD JLLX INT;
  ALTER TABLE dbo.Insp_Maintenance ADD JCJGBH  [varchar](32) 

创建数据库
create database 数据库名;

删除数据库
drop database 数据库名;

联表查询
SELECT 服务器名称||'-'||存储阵列名称||'-'||存储阵列总数 FROM
(
SELECT SERVER.NAME||'('|| SERVER.IP ||')' 服务器名称,
 STORAGE.Name 存储阵列名称,
 COUNT(STORAGE.ID) 存储阵列总数
FROM SERVER
LEFT JOIN SERVERSTORAGE ON SERVERSTORAGE.SERVERID = SERVER.ID
LEFT JOIN STORAGE ON STORAGE.ID = SERVERSTORAGE.STORAGEID
GROUP BY SERVER.NAME||'('|| SERVER.IP ||')',STORAGE.Name
)

平台调用数据类型

下表列出了 Windows API 和 C 样式函数中使用的数据类型。 许多非托管库包含将这些数据类型作为参数和返回值传递的函数。 第三列列出了相应的 .NET Framework 内置值类型或可在托管代码中使用的类。 在某些情况下,你用相同大小的类型替代表中列出的类型。

Windows API 中的非托管类型 非托管 C 语言类型 托管类型 描述
VOID void System.Void 应用于不返回值的函数。
HANDLE void * System.IntPtrSystem.UIntPtr 在 32 位 Windows 操作系统上为 32 位、在 64 位 Windows 操作系统上为 64 位。
BYTE unsigned char System.Byte 8 位
SHORT short System.Int16 16 位
WORD unsigned short System.UInt16 16 位
INT int System.Int32 32 位
UINT unsigned int System.UInt32 32 位
LONG long System.Int32 32 位
BOOL long System.BooleanSystem.Int32 32 位
DWORD unsigned long System.UInt32 32 位
ULONG unsigned long System.UInt32 32 位
CHAR char System.Char 使用 ANSI 修饰。
WCHAR wchar_t System.Char 使用 Unicode 修饰。
LPSTR char * System.StringSystem.Text.StringBuilder 使用 ANSI 修饰。
LPCSTR const char * System.StringSystem.Text.StringBuilder 使用 ANSI 修饰。
LPWSTR wchar_t * System.StringSystem.Text.StringBuilder 使用 Unicode 修饰。
LPCWSTR const wchar_t * System.StringSystem.Text.StringBuilder 使用 Unicode 修饰。
FLOAT float System.Single 32 位
DOUBLE double System.Double 64 位

有关 Visual Basic、C# 和 C++ 中的相应类型,请参阅 .NET Framework 类库简介

工具类

http请求工具 HttpUtils.cs

    using System.Collections.Specialized;
    using System.IO;
    using System.Net;

    class HttpUtils
    {
        public static int CONNECT_TIMEOUT = 3000;
        public static int READ_TIMEOUT = 5000;
        public static int WRITE_TIMEOUT = 5000;
        public static int FILE_TIMEOUT = 60000;
        public static string USER_AGENT = "okhttp/3.12.0";
        public static string ContentType = "application/x-www-form-urlencoded";
        private static readonly Encoding DEFAULTENCODE = Encoding.UTF8;
        /// <summary>
        /// HTTP GET方式请求数据.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            //request.ContentType = "application/x-www-form-urlencoded";
            //request.Accept = "*/*";
            request.UserAgent = USER_AGENT;
            request.ContentType = ContentType;
            request.Timeout = CONNECT_TIMEOUT;
            request.ReadWriteTimeout = READ_TIMEOUT + WRITE_TIMEOUT;
            request.AllowAutoRedirect = false;

            WebResponse response = null;
            string responseStr = null;

            try
            {
                response = request.GetResponse();

                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), DEFAULTENCODE);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                    if (AppConfig.PrintHttp)
                    {
                        Console.WriteLine("--> GET {0} {1}", url, responseStr);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                response = null;
            }

            return responseStr;
        }

        /// <summary>
        /// HTTP POST方式请求数据.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <returns></returns>
        public static string HttpPost(string url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.UserAgent = USER_AGENT;
            request.ContentType = ContentType;
            request.Timeout = CONNECT_TIMEOUT;
            request.ReadWriteTimeout = READ_TIMEOUT + WRITE_TIMEOUT;
            //request.ContentType = "application/json";
            request.ContentLength = DEFAULTENCODE.GetByteCount(postDataStr);
            // request.CookieContainer = cookie;
            Stream myRequestStream = request.GetRequestStream();
            // StreamWriter myStreamWriter = new StreamWriter(myRequestStream, DEFAULTENCODE);
            var bytes = DEFAULTENCODE.GetBytes(postDataStr);
            myRequestStream.Write(bytes, 0, bytes.Length);
            myRequestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            //response.Cookies = cookie.GetCookies(response.ResponseUri);
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, DEFAULTENCODE);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            if (AppConfig.PrintHttp)
            {
                Console.WriteLine("--> POST {0} {1} {2}", url, postDataStr, retString);
            }
            return retString;
        }

        /// <summary>
        /// HttpUploadFile
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string file, NameValueCollection data)
        {
            return HttpUploadFile(url, file, data, DEFAULTENCODE);
        }

        /// <summary>
        /// HttpUploadFile
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string file, NameValueCollection data, Encoding encoding)
        {
            return HttpUploadFile(url, new string[] { file }, data, encoding);
        }

        /// <summary>
        /// HttpUploadFile
        /// </summary>
        /// <param name="url"></param>
        /// <param name="files"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string[] files, NameValueCollection data)
        {
            return HttpUploadFile(url, files, data, DEFAULTENCODE);
        }

        /// <summary>
        /// HttpUploadFile 上传文件 返回
        /// </summary>
        /// <param name="url"></param>
        /// <param name="files"></param>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string[] files, NameValueCollection data, Encoding encoding)
        {
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
            byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");

            //1.HttpWebRequest
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.UserAgent = USER_AGENT;
            request.Timeout = CONNECT_TIMEOUT;
            request.ReadWriteTimeout = READ_TIMEOUT + WRITE_TIMEOUT + FILE_TIMEOUT;
            request.Method = "POST";
            request.KeepAlive = true;
            request.Credentials = CredentialCache.DefaultCredentials;

            using (Stream stream = request.GetRequestStream())
            {
                //1.1 key/value
                string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
                if (data != null)
                {
                    foreach (string key in data.Keys)
                    {
                        stream.Write(boundarybytes, 0, boundarybytes.Length);
                        string formitem = string.Format(formdataTemplate, key, data[key]);
                        byte[] formitembytes = encoding.GetBytes(formitem);
                        stream.Write(formitembytes, 0, formitembytes.Length);
                    }
                }

                //1.2 file
                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                for (int i = 0; i < files.Length; i++)
                {
                    stream.Write(boundarybytes, 0, boundarybytes.Length);
                    string header = string.Format(headerTemplate, "file" + i, Path.GetFileName(files[i]));
                    byte[] headerbytes = encoding.GetBytes(header);
                    stream.Write(headerbytes, 0, headerbytes.Length);
                    using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
                    {
                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            stream.Write(buffer, 0, bytesRead);
                        }
                    }
                }

                //1.3 form end
                stream.Write(endbytes, 0, endbytes.Length);
            }
            //2.WebResponse
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader stream = new StreamReader(response.GetResponseStream(), DEFAULTENCODE))
            {
                return stream.ReadToEnd();
            }
        }

        /// <summary>
        /// Http下载文件 返回下载后的path路径
        /// </summary>
        public static string HttpDownloadFile(string url, string path)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            request.UserAgent = USER_AGENT;
            request.Timeout = CONNECT_TIMEOUT;
            request.ReadWriteTimeout = READ_TIMEOUT + WRITE_TIMEOUT + FILE_TIMEOUT;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream responseStream = response.GetResponseStream();
            //创建本地文件写入流
            Stream stream = new FileStream(path, FileMode.Create);
            byte[] bArr = new byte[1024];
            int size = responseStream.Read(bArr, 0, (int)bArr.Length);
            while (size > 0)
            {
                stream.Write(bArr, 0, size);
                size = responseStream.Read(bArr, 0, (int)bArr.Length);
            }
            stream.Close();
            responseStream.Close();
            return path;
        }
    }

http文件上传

public static class HttpHelper
    {
        private static readonly Encoding DEFAULTENCODE = Encoding.UTF8;

        /// <summary>
        /// HttpUploadFile
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string file, NameValueCollection data)
        {
            return HttpUploadFile(url, file, data, DEFAULTENCODE);
        }

        /// <summary>
        /// HttpUploadFile
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string file, NameValueCollection data, Encoding encoding)
        {
            return HttpUploadFile(url, new string[] { file }, data, encoding);
        }

        /// <summary>
        /// HttpUploadFile
        /// </summary>
        /// <param name="url"></param>
        /// <param name="files"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string[] files, NameValueCollection data)
        {
            return HttpUploadFile(url, files, data, DEFAULTENCODE);
        }

        /// <summary>
        /// HttpUploadFile
        /// </summary>
        /// <param name="url"></param>
        /// <param name="files"></param>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string[] files, NameValueCollection data, Encoding encoding)
        {
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
            byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");

            //1.HttpWebRequest
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.Method = "POST";
            request.KeepAlive = true;
            request.Credentials = CredentialCache.DefaultCredentials;

            using (Stream stream = request.GetRequestStream())
            {
                //1.1 key/value
                string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
                if (data != null)
                {
                    foreach (string key in data.Keys)
                    {
                        stream.Write(boundarybytes, 0, boundarybytes.Length);
                        string formitem = string.Format(formdataTemplate, key, data[key]);
                        byte[] formitembytes = encoding.GetBytes(formitem);
                        stream.Write(formitembytes, 0, formitembytes.Length);
                    }
                }

                //1.2 file
                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                for (int i = 0; i < files.Length; i++)
                {
                    stream.Write(boundarybytes, 0, boundarybytes.Length);
                    string header = string.Format(headerTemplate, "file" + i, Path.GetFileName(files[i]));
                    byte[] headerbytes = encoding.GetBytes(header);
                    stream.Write(headerbytes, 0, headerbytes.Length);
                    using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
                    {
                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            stream.Write(buffer, 0, bytesRead);
                        }
                    }
                }

                //1.3 form end
                stream.Write(endbytes, 0, endbytes.Length);
            }
            //2.WebResponse
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader stream = new StreamReader(response.GetResponseStream()))
            {
                return stream.ReadToEnd();
            }
        }
    }

http文件下载

/// <summary>
/// Http下载文件
/// </summary>

public static string HttpDownloadFile(string url, string path)

{
    // 设置参数
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    //发送请求并获取相应回应数据
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    //直到request.GetResponse()程序才开始向目标网页发送Post请求
    Stream responseStream = response.GetResponseStream();
    //创建本地文件写入流
    Stream stream = new FileStream(path, FileMode.Create);
    byte[] bArr = new byte[1024];
    int size = responseStream.Read(bArr, 0, (int)bArr.Length);
    while (size > 0)
    {
        stream.Write(bArr, 0, size);
        size = responseStream.Read(bArr, 0, (int)bArr.Length);
    }
    stream.Close();
    responseStream.Close();
    return path;
}

FileUtil

class FileUtil
    {
        //C#追加文件
        public static void append(string path, string text)
        {
            StreamWriter sw = File.AppendText(path);
            sw.WriteLine(text);
            sw.Flush();
            sw.Close();
        }

        //C#拷贝文件
        public static void copy(string oldPath, string newPath)
        {
            string OrignFile, NewFile;
            OrignFile = oldPath;
            NewFile = newPath;
            File.Copy(OrignFile, NewFile, true);
        }

        //C#删除文件
        public static void delete(string path)
        {
            string delFile = path;
            File.Delete(delFile);
        }

        //C#移动文件
        public static void move(string oldPath, string newPath)
        {
            string OrignFile, NewFile;
            OrignFile = oldPath;
            NewFile = newPath;
            File.Move(OrignFile, NewFile);
        }

        //C#创建目录
        public static void mkdir(string path)
        {
            // 创建目录c:\sixAge
            DirectoryInfo d = Directory.CreateDirectory(path);
            // d1指向c:\sixAge\sixAge1
            //DirectoryInfo d1 = d.CreateSubdirectory("sixAge1");
            // d2指向c:\sixAge\sixAge1\sixAge1_1
            //DirectoryInfo d2 = d1.CreateSubdirectory("sixAge1_1");
            // 将当前目录设为c:\sixAge
            //Directory.SetCurrentDirectory("c:\\sixAge");
            //创建目录c:\sixAge\sixAge2
            //Directory.CreateDirectory("sixAge2");
            // 创建目录c:\sixAge\sixAge2\sixAge2_1
            //Directory.CreateDirectory("sixAge2\\sixAge2_1");
        }

        //递归删除文件夹及文件
        public void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir)) //如果存在这个文件夹删除之
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                        File.Delete(d); //直接删除其中的文件
                    else
                        DeleteFolder(d); //递归删除子文件夹
                }
                Directory.Delete(dir); //删除已空文件夹
                Console.WriteLine(dir + " 文件夹删除成功");
            }
            else
                Console.WriteLine(dir + " 该文件夹不存在"); //如果文件夹不存在则提示
        }

        // ======================================================
        // 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面
        // 如果目标文件夹为只读属性就会报错。
        // April 18April2005 In STU
        // ======================================================
        public static void CopyDir(string srcPath, string aimPath)
        {

            try
            {

                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 判断目标目录是否存在如果不存在则新建之
                if (!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
                // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
                // string[] fileList = Directory.GetFiles(srcPath);
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                // 遍历所有的文件和目录
                foreach (string file in fileList)
                {

                    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (Directory.Exists(file))
                        CopyDir(file, aimPath + Path.GetFileName(file));
                    // 否则直接Copy文件
                    else
                        File.Copy(file, aimPath + Path.GetFileName(file), true);
                }
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }
        }

        // ======================================================
        // 实现一个静态方法将指定文件夹下面的所有内容Detele
        // 测试的时候要小心操作,删除之后无法恢复。
        // April 18April2005 In STU
        // ======================================================
        public static void DeleteDir(string aimPath)
        {

            try
            {

                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                // 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
                // string[] fileList = Directory.GetFiles(aimPath);
                string[] fileList = Directory.GetFileSystemEntries(aimPath);
                // 遍历所有的文件和目录
                foreach (string file in fileList)
                {

                    // 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件
                    if (Directory.Exists(file))
                    {

                        DeleteDir(aimPath + Path.GetFileName(file));
                    }
                    // 否则直接Delete文件
                    else
                    {

                        File.Delete(aimPath + Path.GetFileName(file));
                    }
                }
                //删除文件夹
                System.IO.Directory.Delete(aimPath, true);
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }
        }
        //获取文件大小
        public static int GetFileSize(string path)
        {
            System.IO.FileInfo fileInfo = null;
            try
            {
                fileInfo = new System.IO.FileInfo(path);
            }
            catch
            {
                return 0;
            }
            if (fileInfo != null && fileInfo.Exists)
            {
                return (int)System.Math.Ceiling(fileInfo.Length / 1024.0);
            }
            else
            {
                return 0;
            }
        }
    }

JsonReadWrite json读取和写入

 //json读取和写入
    class JsonReadWrite
    {

        //将类写入为json数据
        public static bool WriteObject(object obj, string path)
        {
            string jsonStr = JsonConvert.SerializeObject(obj);
            try
            {
                FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
                byte[] buffer = Encoding.UTF8.GetBytes(jsonStr);
                stream.Write(buffer, 0, buffer.Length);
                stream.Close();
                return true;
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
            }
            return false;
        }

        //获取文件大小
        private static long GetFileSize(string path)
        {
            System.IO.FileInfo fileInfo = null;
            try
            {
                fileInfo = new System.IO.FileInfo(path);
            }
            catch
            {
                return 0;
            }
            if (fileInfo != null && fileInfo.Exists)
            {
                return fileInfo.Length;
                //return (int)System.Math.Ceiling(fileInfo.Length / 1024.0);
            }
            else
            {
                return 0;
            }
        }

        //将json数据读取到类
        public static T ReadObject<T>(string path)
        {
            int len = (int)GetFileSize(path);
            if (len > 0)
            {
                byte[] buffer = new byte[len];
                try
                {
                    FileStream stream = new FileStream(path, FileMode.Open);
                    stream.Read(buffer, 0, len);
                    stream.Close();
                    return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(buffer));
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message + "\n Cannot open file.");
                    return default(T);
                }

            }
            else
            {
                Console.WriteLine("file len < 0");
                return default(T);
            }
        }
    }

将json和对象互相转换

LoginModel loginmodel = JsonConvert.DeserializeObject<LoginModel>(reString);
string json_str = Newtonsoft.Json.JsonConvert.SerializeObject(Params, Formatting.Indented);
// JSON序列化
string  result  =  JsonConvert.SerializeObject(user);

UrlEncode

public static string UrlEncode(string str, Encoding coding) //System.Text.Encoding.UTF8
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = coding.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                if ((byStr[i] >= 'a' && byStr[i] <= 'z') || (byStr[i] >= 'A' && byStr[i] <= 'Z') || (byStr[i] >= '0' && byStr[i] <= '9'))
                {
                    sb.Append((char)byStr[i]);
                }
                else if (byStr[i] == ' ')
                {
                    sb.Append('+');
                }
                else
                {
                    sb.Append(@"%" + Convert.ToString(byStr[i], 16));
                }

            }

            return (sb.ToString());
        }

Hashtable转json

 class HashToJson
    {
        #region 方法 HashtableToJSON(Hashtable data) Hashtable表转JSON
        /// <summary>
        /// Hashtable表转JSON 支持多层Hashtable嵌套
        /// </summary>
        /// <param name="data"></param>
        /// <returns>var postData = new Hashtable();postData.Add("openid", "55");postData.Add("card_id", "55");</returns>
        public static string HashtableToJSON(Hashtable data)
        {

            try
            {
                   StringBuilder sb = new StringBuilder();
                sb.Append("{");
                foreach (object key in data.Keys)
                {

                    object value = data[key];
                    sb.Append("\"");
                    sb.Append(key);
                    sb.Append("\":");

                    if (!string.IsNullOrEmpty(value.ToString()) && value != DBNull.Value)
                    {
                        if(value is string)
                        {
                            sb.Append("\"");
                            string newValue = (string)value;
                            newValue = EnableJsonString(newValue);
                            sb.Append(newValue);

                            sb.Append("\"");
                        }
                        else if(value is Hashtable)
                        {
                            sb.Append(HashtableToJSON((Hashtable)value));
                        }
                        else if (value is float || value is double)
                        {
                            sb.Append(value.ToString());
                        }
                        else if(value is int || value is long)
                        {
                            sb.Append(value.ToString());
                        }
                        else if(value is bool)
                        {
                            sb.Append(value.ToString());
                        }
                        else if(value is string[])
                        {
                            string[] strs = (string[])value;
                            sb.Append("[");
                            for(int i=0;i<strs.Length;i++)
                            {
                                sb.Append(EnableJsonString(strs[i]));
                                if (i < strs.Length - 1)
                                {
                                    sb.Append(",");
                                }
                            }
                            sb.Append("]");
                        }
                        else if(value is int[])
                        {
                            int[] values = (int[])value;
                            sb.Append("[");
                            for (int i = 0; i < values.Length; i++)
                            {
                                sb.Append(values[i].ToString());
                                if (i < values.Length - 1)
                                {
                                    sb.Append(",");
                                }
                            }
                            sb.Append("]");
                        }
                        else if (value is long[])
                        {
                            long[] values = (long[])value;
                            sb.Append("[");
                            for (int i = 0; i < values.Length; i++)
                            {
                                sb.Append(values[i].ToString());
                                if (i < values.Length - 1)
                                {
                                    sb.Append(",");
                                }
                            }
                            sb.Append("]");
                        }
                        else if(value is double[])
                        {
                            double[] values = (double[])value;
                            sb.Append("[");
                            for (int i = 0; i < values.Length; i++)
                            {
                                sb.Append(values[i].ToString());
                                if (i < values.Length - 1)
                                {
                                    sb.Append(",");
                                }
                            }
                            sb.Append("]");
                        }
                        else if(value is Hashtable[])
                        {
                            Hashtable[] tables = (Hashtable[])value;
                            sb.Append("[");
                            for (int i = 0; i < tables.Length; i++)
                            {
                                sb.Append(HashtableToJSON(tables[i]));
                                if (i < tables.Length - 1)
                                {
                                    sb.Append(",");
                                }
                            }
                            sb.Append("]");
                        }
                        else
                        {
                            sb.Append(value.ToString());
                        }
                    }
                    else
                    {

                        sb.Append(" ");
                    }
                    sb.Append(",");
                }
                sb = sb.Remove(sb.Length - 1, 1);
                sb.Append("}");
                return sb.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return "";
            }
        }
        #endregion

        private static string EnableJsonString(string newValue)
        {
            newValue = newValue.Replace("\\", "\\\\");
            newValue = newValue.Replace("\"", "\\\"");
            newValue = newValue.Replace("/", "\\/");
            newValue = newValue.Replace(" ", "\\b");
            newValue = newValue.Replace("\n", "\\n");
            newValue = newValue.Replace("\r", "\\r");
            newValue = newValue.Replace("\t", "\\t");
            newValue = newValue.Replace("\f", "\\f");
            return newValue;
        }
    }

ByteBuffer.cs

/**
     * 可无限制增长的字节缓冲区
     * @author wangchen11
     *
     */
    public class ByteBuffer
    {
        //默认起始缓冲区长度
        public static int DEFAULT_START_SIZE = 64;
        //默认每次增加缓冲区的长度
        public static int DEFAULT_STEP_SIZE = 512;
        private byte[] mBuffer = null;
        private int mPos = 0;
        private int mStepSize = 0;

        public ByteBuffer()
        {
            init(DEFAULT_START_SIZE, DEFAULT_STEP_SIZE);
        }

        public ByteBuffer(int startSize)
        {
            init(startSize, DEFAULT_STEP_SIZE);
        }

        public ByteBuffer(int startSize, int stepSize)
        {
            init(startSize, stepSize);
        }

        private void init(int startSize, int stepSize)
        {
            setRealSize(startSize);
            setStepSize(stepSize);
        }

        public void setStepSize(int stepSize)
        {
            if (stepSize < 1)
                stepSize = 1;
            mStepSize = stepSize;
        }

        public void put(byte data)
        {
            checkSize(mPos + 1);
            mBuffer[mPos] = data;
            mPos++;
        }

        public byte get(int pos)
        {
            return mBuffer[pos];
        }

        public void put(byte[] data)
        {
            put(data, 0, data.Length);
        }

        public void put(byte[] data, int offset, int count)
        {
            checkSize(mPos + count);
            System.Array.Copy(data, offset, mBuffer, mPos, count);
            mPos += count;
        }

        private void checkSize(int needSize)
        {
            if (needSize > mBuffer.Length)
            {
                setRealSize(needSize + mStepSize);
            }
        }

        private void setRealSize(int realSize)
        {
            byte[] tempBuffer = new byte[realSize];
            if (mBuffer != null)
            {
                System.Array.Copy(mBuffer, 0, tempBuffer, 0, mPos);
            }
            mBuffer = tempBuffer;
        }

        public void clear()
        {
            mPos = 0;
        }

        //删除指定长度字节
        public void delete(int len)
        {
            if (len <= mPos)
            {
                byte[] bytes = new byte[mBuffer.Length];
                System.Array.Copy(mBuffer, len, bytes, 0, mBuffer.Length - len);
                mBuffer = bytes;
                mPos -= len;
            }
        }

        public int length()
        {
            return mPos;
        }

        public byte[] getBytes()
        {
            byte[] bytes = new byte[mPos];
            System.Array.Copy(mBuffer, 0, bytes, 0, mPos);
            return bytes;
        }
    }

网络操作相关类NetHelper

using System;
using System.Text;
using System.Net.Sockets;
using System.Net.Mail;
using System.Net;

namespace DotNet.Utilities
{
    /// <summary>
    /// 网络操作相关的类
    /// </summary>    
    public class NetHelper
    {
        #region 检查设置的IP地址是否正确,返回正确的IP地址
        /// <summary>
        /// 检查设置的IP地址是否正确,并返回正确的IP地址,无效IP地址返回"-1"。
        /// </summary>
        /// <param name="ip">设置的IP地址</param>
        //public static string GetValidIP(string ip)
        //{
        //    if (PageValidate.IsIP(ip))
        //    {
        //        return ip;
        //    }
        //    else
        //    {
        //        return "-1";
        //    }
        //}
        #endregion

        #region 检查设置的端口号是否正确,返回正确的端口号
        /// <summary>
        /// 检查设置的端口号是否正确,并返回正确的端口号,无效端口号返回-1。
        /// </summary>
        /// <param name="port">设置的端口号</param>        
        public static int GetValidPort(string port)
        {
            //声明返回的正确端口号
            int validPort = -1;
            //最小有效端口号
            const int MINPORT = 0;
            //最大有效端口号
            const int MAXPORT = 65535;

            //检测端口号
            try
            {
                //传入的端口号为空则抛出异常
                if (port == "")
                {
                    throw new Exception("端口号不能为空!");
                }

                //检测端口范围
                if ((Convert.ToInt32(port) < MINPORT) || (Convert.ToInt32(port) > MAXPORT))
                {
                    throw new Exception("端口号范围无效!");
                }

                //为端口号赋值
                validPort = Convert.ToInt32(port);
            }
            catch (Exception ex)
            {
                string errMessage = ex.Message;
            }
            return validPort;
        }
        #endregion

        #region 将字符串形式的IP地址转换成IPAddress对象
        /// <summary>
        /// 将字符串形式的IP地址转换成IPAddress对象
        /// </summary>
        /// <param name="ip">字符串形式的IP地址</param>        
        public static IPAddress StringToIPAddress(string ip)
        {
            return IPAddress.Parse(ip);
        }
        #endregion

        #region 获取本机的计算机名
        /// <summary>
        /// 获取本机的计算机名
        /// </summary>
        public static string LocalHostName
        {
            get
            {
                return Dns.GetHostName();
            }
        }
        #endregion

        #region 获取本机的局域网IP
        /// <summary>
        /// 获取本机的局域网IP
        /// </summary>        
        public static string LANIP
        {
            get
            {
                //获取本机的IP列表,IP列表中的第一项是局域网IP,第二项是广域网IP
                IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
                string ip = "";
                for(int i = 0; i < addressList.Length; i++)
                {
                    if (addressList[i].ToString().IndexOf(":") == -1)
                    {
                        ip = addressList[i].ToString();
                    }
                    Console.WriteLine("{0} {1}", addressList[i].ToString(), addressList[i].IsIPv6Teredo);
                }
                //如果本机IP列表为空,则返回空字符串
                if (addressList.Length < 1)
                {
                    return "";
                }

                //返回本机的局域网IP
                return ip;
            }
        }
        #endregion

        #region 获取本机在Internet网络的广域网IP
        /// <summary>
        /// 获取本机在Internet网络的广域网IP
        /// </summary>        
        public static string WANIP
        {
            get
            {
                //获取本机的IP列表,IP列表中的第一项是局域网IP,第二项是广域网IP
                IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;

                //如果本机IP列表小于2,则返回空字符串
                if (addressList.Length < 2)
                {
                    return "";
                }

                //返回本机的广域网IP
                return addressList[1].ToString();
            }
        }
        #endregion

        #region 获取远程客户机的IP地址
        /// <summary>
        /// 获取远程客户机的IP地址
        /// </summary>
        /// <param name="clientSocket">客户端的socket对象</param>        
        public static string GetClientIP(Socket clientSocket)
        {
            IPEndPoint client = (IPEndPoint)clientSocket.RemoteEndPoint;
            return client.Address.ToString();
        }
        #endregion

        #region 创建一个IPEndPoint对象
        /// <summary>
        /// 创建一个IPEndPoint对象
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口号</param>        
        public static IPEndPoint CreateIPEndPoint(string ip, int port)
        {
            IPAddress ipAddress = StringToIPAddress(ip);
            return new IPEndPoint(ipAddress, port);
        }
        #endregion

        #region 创建一个TcpListener对象
        /// <summary>
        /// 创建一个自动分配IP和端口的TcpListener对象
        /// </summary>        
        public static TcpListener CreateTcpListener()
        {
            //创建一个自动分配的网络节点
            IPAddress ipAddress = IPAddress.Any;
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 0);

            return new TcpListener(localEndPoint);
        }
        /// <summary>
        /// 创建一个TcpListener对象
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口</param>        
        public static TcpListener CreateTcpListener(string ip, int port)
        {
            //创建一个网络节点
            IPAddress ipAddress = StringToIPAddress(ip);
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

            return new TcpListener(localEndPoint);
        }
        #endregion

        #region 创建一个基于TCP协议的Socket对象
        /// <summary>
        /// 创建一个基于TCP协议的Socket对象
        /// </summary>        
        public static Socket CreateTcpSocket()
        {
            return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
        #endregion

        #region 创建一个基于UDP协议的Socket对象
        /// <summary>
        /// 创建一个基于UDP协议的Socket对象
        /// </summary>        
        public static Socket CreateUdpSocket()
        {
            return new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        }
        #endregion

        #region 获取本地终结点

        #region 获取TcpListener对象的本地终结点
        /// <summary>
        /// 获取TcpListener对象的本地终结点
        /// </summary>
        /// <param name="tcpListener">TcpListener对象</param>        
        public static IPEndPoint GetLocalPoint(TcpListener tcpListener)
        {
            return (IPEndPoint)tcpListener.LocalEndpoint;
        }

        /// <summary>
        /// 获取TcpListener对象的本地终结点的IP地址
        /// </summary>
        /// <param name="tcpListener">TcpListener对象</param>        
        public static string GetLocalPoint_IP(TcpListener tcpListener)
        {
            IPEndPoint localEndPoint = (IPEndPoint)tcpListener.LocalEndpoint;
            return localEndPoint.Address.ToString();
        }

        /// <summary>
        /// 获取TcpListener对象的本地终结点的端口号
        /// </summary>
        /// <param name="tcpListener">TcpListener对象</param>        
        public static int GetLocalPoint_Port(TcpListener tcpListener)
        {
            IPEndPoint localEndPoint = (IPEndPoint)tcpListener.LocalEndpoint;
            return localEndPoint.Port;
        }
        #endregion

        #region 获取Socket对象的本地终结点
        /// <summary>
        /// 获取Socket对象的本地终结点
        /// </summary>
        /// <param name="socket">Socket对象</param>        
        public static IPEndPoint GetLocalPoint(Socket socket)
        {
            return (IPEndPoint)socket.LocalEndPoint;
        }

        /// <summary>
        /// 获取Socket对象的本地终结点的IP地址
        /// </summary>
        /// <param name="socket">Socket对象</param>        
        public static string GetLocalPoint_IP(Socket socket)
        {
            IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
            return localEndPoint.Address.ToString();
        }

        /// <summary>
        /// 获取Socket对象的本地终结点的端口号
        /// </summary>
        /// <param name="socket">Socket对象</param>        
        public static int GetLocalPoint_Port(Socket socket)
        {
            IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
            return localEndPoint.Port;
        }
        #endregion

        #endregion

        #region 绑定终结点
        /// <summary>
        /// 绑定终结点
        /// </summary>
        /// <param name="socket">Socket对象</param>
        /// <param name="endPoint">要绑定的终结点</param>
        public static void BindEndPoint(Socket socket, IPEndPoint endPoint)
        {
            if (!socket.IsBound)
            {
                socket.Bind(endPoint);
            }
        }

        /// <summary>
        /// 绑定终结点
        /// </summary>
        /// <param name="socket">Socket对象</param>        
        /// <param name="ip">服务器IP地址</param>
        /// <param name="port">服务器端口</param>
        public static void BindEndPoint(Socket socket, string ip, int port)
        {
            //创建终结点
            IPEndPoint endPoint = CreateIPEndPoint(ip, port);

            //绑定终结点
            if (!socket.IsBound)
            {
                socket.Bind(endPoint);
            }
        }
        #endregion

        #region 指定Socket对象执行监听
        /// <summary>
        /// 指定Socket对象执行监听,默认允许的最大挂起连接数为100
        /// </summary>
        /// <param name="socket">执行监听的Socket对象</param>
        /// <param name="port">监听的端口号</param>
        public static void StartListen(Socket socket, int port)
        {
            //创建本地终结点
            IPEndPoint localPoint = CreateIPEndPoint(NetHelper.LocalHostName, port);

            //绑定到本地终结点
            BindEndPoint(socket, localPoint);

            //开始监听
            socket.Listen(100);
        }

        /// <summary>
        /// 指定Socket对象执行监听
        /// </summary>
        /// <param name="socket">执行监听的Socket对象</param>
        /// <param name="port">监听的端口号</param>
        /// <param name="maxConnection">允许的最大挂起连接数</param>
        public static void StartListen(Socket socket, int port, int maxConnection)
        {
            //创建本地终结点
            IPEndPoint localPoint = CreateIPEndPoint(NetHelper.LocalHostName, port);

            //绑定到本地终结点
            BindEndPoint(socket, localPoint);

            //开始监听
            socket.Listen(maxConnection);
        }

        /// <summary>
        /// 指定Socket对象执行监听
        /// </summary>
        /// <param name="socket">执行监听的Socket对象</param>
        /// <param name="ip">监听的IP地址</param>
        /// <param name="port">监听的端口号</param>
        /// <param name="maxConnection">允许的最大挂起连接数</param>
        public static void StartListen(Socket socket, string ip, int port, int maxConnection)
        {
            //绑定到本地终结点
            BindEndPoint(socket, ip, port);

            //开始监听
            socket.Listen(maxConnection);
        }
        #endregion

        #region 连接到基于TCP协议的服务器
        /// <summary>
        /// 连接到基于TCP协议的服务器,连接成功返回true,否则返回false
        /// </summary>
        /// <param name="socket">Socket对象</param>
        /// <param name="ip">服务器IP地址</param>
        /// <param name="port">服务器端口号</param>     
        public static bool Connect(Socket socket, string ip, int port)
        {
            try
            {
                //连接服务器
                socket.Connect(ip, port);

                //检测连接状态
                return socket.Poll(-1, SelectMode.SelectWrite);
            }
            catch (SocketException ex)
            {
                throw new Exception(ex.Message);
                //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
            }
        }
        #endregion

        #region 以同步方式发送消息
        /// <summary>
        /// 以同步方式向指定的Socket对象发送消息
        /// </summary>
        /// <param name="socket">socket对象</param>
        /// <param name="msg">发送的消息</param>
        public static void SendMsg(Socket socket, byte[] msg)
        {
            //发送消息
            socket.Send(msg, msg.Length, SocketFlags.None);
        }

        /// <summary>
        /// 使用UTF8编码格式以同步方式向指定的Socket对象发送消息
        /// </summary>
        /// <param name="socket">socket对象</param>
        /// <param name="msg">发送的消息</param>
        public static void SendMsg(Socket socket, string msg)
        {
            //将字符串消息转换成字符数组
            byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(msg);

            //发送消息
            socket.Send(buffer, buffer.Length, SocketFlags.None);
        }
        #endregion

        #region 以同步方式接收消息
        /// <summary>
        /// 以同步方式接收消息
        /// </summary>
        /// <param name="socket">socket对象</param>
        /// <param name="buffer">接收消息的缓冲区</param>
        public static void ReceiveMsg(Socket socket, byte[] buffer)
        {
            socket.Receive(buffer);
        }

        /// <summary>
        /// 以同步方式接收消息,并转换为UTF8编码格式的字符串,使用5000字节的默认缓冲区接收。
        /// </summary>
        /// <param name="socket">socket对象</param>        
        public static string ReceiveMsg(Socket socket)
        {
            //定义接收缓冲区
            byte[] buffer = new byte[5000];
            //接收数据,获取接收到的字节数
            int receiveCount = socket.Receive(buffer);

            //定义临时缓冲区
            byte[] tempBuffer = new byte[receiveCount];
            //将接收到的数据写入临时缓冲区
            Buffer.BlockCopy(buffer, 0, tempBuffer, 0, receiveCount);
            //转换成字符串,并将其返回

            return System.Text.Encoding.UTF8.GetString(tempBuffer);
        }
        #endregion

        #region 关闭基于Tcp协议的Socket对象
        /// <summary>
        /// 关闭基于Tcp协议的Socket对象
        /// </summary>
        /// <param name="socket">要关闭的Socket对象</param>
        public static void Close(Socket socket)
        {
            try
            {
                //禁止Socket对象接收和发送数据
                socket.Shutdown(SocketShutdown.Both);
            }
            catch (SocketException ex)
            {
                throw ex;
            }
            finally
            {
                //关闭Socket对象
                socket.Close();
            }
        }
        #endregion

        #region 发送电子邮件
        /// <summary>
        /// 发送电子邮件,所有SMTP配置信息均在config配置文件中system.net节设置.
        /// </summary>
        /// <param name="receiveEmail">接收电子邮件的地址</param>
        /// <param name="msgSubject">电子邮件的标题</param>
        /// <param name="msgBody">电子邮件的正文</param>
        /// <param name="IsEnableSSL">是否开启SSL</param>
        public static bool SendEmail(string receiveEmail, string msgSubject, string msgBody, bool IsEnableSSL)
        {
            //创建电子邮件对象
            MailMessage email = new MailMessage();
            //设置接收人的电子邮件地址
            email.To.Add(receiveEmail);
            //设置邮件的标题
            email.Subject = msgSubject;
            //设置邮件的正文
            email.Body = msgBody;
            //设置邮件为HTML格式
            email.IsBodyHtml = true;

            //创建SMTP客户端,将自动从配置文件中获取SMTP服务器信息
            SmtpClient smtp = new SmtpClient();
            //开启SSL
            smtp.EnableSsl = IsEnableSSL;

            try
            {
                //发送电子邮件
                smtp.Send(email);

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}

检测是否有管理员权限

public static bool IsAdministrator()
        {
            WindowsIdentity current = WindowsIdentity.GetCurrent();
            WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
            //WindowsBuiltInRole可以枚举出很多权限,例如系统用户、User、Guest等等
            return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
        }

以管理员权限运行程序

Win WPF教程

https://blog.csdn.net/weixin_34343308/article/details/85716725

vs开发工具使用技巧:https://www.cnblogs.com/lihuali/p/7234230.html

Sql Server

Sql Server Manager 操作

Alt+X 执行当前操作 / 刷新列表

修改表中单行数据

set BeginTime = '2022-04-27 09:30:27',
EndTime = '2022-04-27 09:30:52'
where Id = 43

查询表

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT TOP 1000 [Id],[JCXDH],[SBBH],[BeginTime],[EndTime],[QTNDLX],[HC_SDZ],[HC_CLZ],[HC_JDWC],[HC_JDXZ] ,[HC_XDWC] ,[HC_XDXZ] ,[HC_JDPD] ,[HC_XDPD]  ,[HC_PDJG]  ,[CO_SDZ] ,[CO_CLZ] ,[CO_JDWC] ,[CO_JDXZ] ,[CO_XDWC],[CO_XDXZ],[CO_JDPD] ,[CO_XDPD] ,[CO_PDJG] ,[CO2_SDZ] ,[CO2_CLZ] ,[CO2_JDWC] ,[CO2_JDXZ],[CO2_XDWC] ,[CO2_XDXZ],[CO2_JDPD] ,[CO2_XDPD],[CO2_PDJG],[NO_SDZ],[NO_CLZ] ,[NO_JDWC],[NO_JDXZ] ,[NO_XDWC],[NO_XDXZ],[NO_JDPD],[NO_XDPD],[NO_PDJG],[O2_SDZ],[O2_CLZ],[O2_JDWC],[O2_JDXZ],[O2_XDWC],[O2_XDXZ],[O2_JDPD],[O2_XDPD],[O2_PDJG],[GCSJJson] ,[PDJG] ,[CZR] ,[Extend1],[Extend2]
      ,[Extend3]
      ,[Extend4]
      ,[HC_T90]
      ,[HC_T95]
      ,[HC_T10]
      ,[HC_T5]
      ,[HC_T90PD]
      ,[HC_T95PD]
      ,[HC_T10PD]
      ,[HC_T5PD]
      ,[CO_T90]
      ,[CO_T95]
      ,[CO_T10]
      ,[CO_T5]
      ,[CO_T90PD]
      ,[CO_T95PD]
      ,[CO_T10PD]
      ,[CO_T5PD]
      ,[CO2_T90]
      ,[CO2_T95]
      ,[CO2_T10]
      ,[CO2_T5]
      ,[CO2_T90PD] ,[CO2_T95PD] ,[CO2_T10PD] ,[CO2_T5PD] ,[NO_T90] ,[NO_T95] ,[NO_T10] ,[NO_T5]  ,[NO_T90PD] ,[NO_T95PD] ,[NO_T10PD] ,[NO_T5PD] ,[O2_T90],[O2_T95],[O2_T10],[O2_T5] ,[O2_T90PD]
      ,[O2_T95PD],[O2_T10PD],[O2_T5PD]
  FROM [HBXGB_2019].[dbo].[Insp_FQFXY_LowGas]
  where BeginTime > '2022-04-27'

未知内容

/// <summary>
        /// 实体转Json .00转为int
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJsonZroe<T>(this T obj)
        {
            System.Web.Script.Serialization.JavaScriptSerializer sr = new System.Web.Script.Serialization.JavaScriptSerializer();
            return sr.Serialize(obj);
        }

out 关键字

"

.resx文件

NetworkCredential

new System.Diagnostics.StackTrace()

fixed:在一个代码块执行时,在固定内存位置为一个变量指派一个指针。

checked:既是操作符又是语句,确保编译器运行时,检查整数类型操作或转换时出现的溢出。

internal:一个访问修饰符。

operator:用来声明或多载一个操作符。

out:标识一个参数值会受影响的参数,但在传入方法时,该参数无需先初始化。

params:声明一个参数数组。如果使用,必须修改指定的最后一个参数,允许可选参数。

readonly:标识一个变量的值在初始化后不可修改。

ref:标识一个参数值可能会受影响的参数。

sealed:防止类型被派生,防止方法和property被覆载。

stackalloc:返回在堆上分配的一个内存块的指针。

truct:是一种值类型,可以声明常量、字段、方法、property、索引器、操作符、构造器和内嵌类型。

unchecked:禁止溢出检查。

unsafe:标注包含指针操作的代码块、方法或类。

virtual:一个方法修饰符,标识可被覆载的方法。

volatile:标识一个可被操作系统、某些硬件设备或并发线程修改的attribute

其它

donet 启用HTTPS

dotnet dev-certs https -ep $env:USERPROFILE\.aspnet\https\aspnetapp.pfx -p crypticpassword
dotnet dev-certs https --trust

https://docs.microsoft.com/zh-cn/dotnet/core/additional-tools/self-signed-certificates-guide

发表评论

邮箱地址不会被公开。 必填项已用*标注