工作中常用到的一些方法C#

  1. 应用程序 防止 重复开启
 bool canCreateNew;
 Mutex m_ServerMutex = new Mutex(true, "EMTASS_SERVER", out  canCreateNew);
 if (canCreateNew) { }
 else{ }
  1. string类的常用方法:

    string.length 计算的值从1开始算
    string.indexof("") 从0开始
    string.substring(a,b);从第a位开始,取b个字符
    string.split(’ ') 以" "为分界将字符串划分为N个数组

  2. 配置打印机

using System.Threading;
using System.IO;
using System.IO.Ports;
        /// <summary>
        /// 配置打印机
        /// </summary>
        ///
        private void Print(string content)
        {
            Thread.Sleep(100);
            string printTxt1 = "";
            string path1 = System.IO.Directory.GetCurrentDirectory();
            FileStream tempFile1 = new FileStream(path1 + @"\Text.txt",  FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamReader tempFileRead1 = new StreamReader(tempFile1,  Encoding.GetEncoding("gb2312"));
            printTxt1 = tempFileRead1.ReadToEnd();
            tempFileRead1.Close();
            tempFile1.Close();
            SerialPort sp1 = new SerialPort();
            sp1.BaudRate = 9600;  
            sp1.PortName = "COM1";  //使用COM1端口
            sp1.Parity = System.IO.Ports.Parity.None;
            sp1.DataBits = 8;
            sp1.StopBits = System.IO.Ports.StopBits.One;
            sp1.Open();
            try
            {
                Thread.Sleep(10);
                sp1.WriteLine(printTxt1);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            sp1.Close();
        }
上一篇:【解决】表单验证登录后无法进行搜索


下一篇:ZZULIOJ 1085: 求奇数的乘积(多实例测试),Java