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

版权声明:@今年依旧12岁 https://blog.csdn.net/qq_34087199/article/details/89494710
  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();
        }

猜你喜欢

转载自blog.csdn.net/qq_34087199/article/details/89494710