小常识

1.程序中启动其他进程     

string path = Application.StartupPath + "\\**.exe";
Process.Start(path);


2.事件触发

 private void ButtonClick(object sender, RoutedEventArgs e)
 {            
   // Get the current button.
   Button cmd = (Button)e.OriginalSource;                
   // Create an instance of the window named
   // by the current button.
   Type type = this.GetType();
   Assembly assembly = type.Assembly;                       
   Window win = (Window)assembly.CreateInstance(
         type.Namespace + "." + cmd.Content);            
   // Show the window.
   win.ShowDialog();
 }

3.ini配置文件读写类

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Ini
{
    public class IniFile
    {
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public IniFile(string INIPath)
        {
            path = INIPath;
        }
        
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }
    }
}


4.CRC校验(多项式A001)http://download.csdn.net/detail/wohaorende/3597075

        public string  GetCRC(string DATA)
        {
            //byte[] buffer = Encoding.ASCII.GetBytes("Normal");
            long functionReturnValue = 0;

            long i = 0;
            long J = 0;
            byte[] v = null;
            //v = strToToHexByte(DATA);
            v = Encoding.ASCII.GetBytes(DATA);

            //1.预置1个16位的寄存器为十六进制FFFF(即全为1):称此寄存器为CRC寄存器;
            long CRC = 0;
            CRC = 0xffffL;
            for (i = 0; i <= (v).Length - 1; i++)
            {
                //2.把第一个8位二进制数据(既通讯信息帧的第一个字节)与16位的CRC寄存器的低8位相异或,把结果放于CRC寄存器;
                CRC = (CRC / 256) * 256L + (CRC % 256L) ^ v[i];
                for (J = 0; J <= 7; J++)
                {
                    //3.把CRC寄存器的内容右移一位(朝低位)用0填补最高位,并检查最低位;
                    //4.如果最低位为0:重复第3步(再次右移一位);
                    // 如果最低位为1:CRC寄存器与多项式A001(1010 0000 0000 0001)进行异或;
                    //5.重复步骤3和4,直到右移8次,这样整个8位数据全部进行了处理;
                    long d0 = 0;
                    d0 = CRC & 1L;
                    CRC = CRC / 2;
                    if (d0 == 1)
                        CRC = CRC ^ 0xa001L;
                }
                //6.重复步骤2到步骤5,进行通讯信息帧下一字节的处理;
            }

            //7.最后得到的CRC寄存器内容即为:CRC码。
            CRC = CRC % 65536;
            functionReturnValue = CRC;
            //return functionReturnValue.ToString();
            return string.Format("{0:X}", functionReturnValue);
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值