INIFileHelper

 class INIFileHelper
    {
        [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);


        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);


        /// <summary>  
        /// 写值
        /// </summary>  
        /// <param name="Section"></param>  
        /// <param name="Key"></param>  
        /// <param name="Value"></param>  
        public static void IniWriteValue(string Section, string Key, string Value, string path)
        {
            WritePrivateProfileString(Section, Key, Value, path);
        }

        /// <summary>  
        /// 通过key读取相应的value
        /// </summary>  
        /// <param name="Section"></param>  
        /// <param name="Key"></param>  
        /// <returns></returns>  
        public static string IniReadValue(string Section, string Key, string path)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            return temp.ToString();
        }
        public static string IniReadValue(string Key)
        {
            string fileName = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WK_Framework_Config"), "PintSet");
            if (!Directory.Exists(fileName))
            {
                //创建日志文件夹
                Directory.CreateDirectory(fileName);
            }
            fileName += "\\" + "SystemPrint" + ".ini";
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(LoginInfo.AddressIP, Key, "", temp, 255, fileName);
            return temp.ToString();
        }
        public static byte[] IniReadValues(string section, string key, string path)
        {
            byte[] temp = new byte[255];
            int i = GetPrivateProfileString(section, key, "", temp, 255, path);
            return temp;

        }
        /// <summary>  
        /// 清空所有值  
        /// </summary>  
        public static void ClearAllSection(string path)
        {
            IniWriteValue(null, null, null, path);
        }
        /// <summary>  
        /// 清空指定key的value
        /// </summary>  
        /// <param name="Section"></param>  
        public static void ClearSection(string Section, string path)
        {
            IniWriteValue(Section, null, null, path);
        }
        public static void ClearSection(string Section)
        {
            string fileName = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WK_Framework_Config"), "PintSet");
            if (!Directory.Exists(fileName))
            {
                //创建日志文件夹
                Directory.CreateDirectory(fileName);
            }
            fileName += "\\" + "SystemPrint" + ".ini";
            IniWriteValue(Section, null, null, fileName);
        }

        public static void Write(string key, string value)
        {
            string fileName = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WK_Framework_Config"), "PintSet");
            if (!Directory.Exists(fileName))
            {
                //创建日志文件夹
                Directory.CreateDirectory(fileName);
            }
            fileName += "\\" + "SystemPrint" + ".ini";
            INIFileHelper.IniWriteValue(LoginInfo.AddressIP, key, value, fileName);
        }
        public static void GetPrintName(string key, string value, ref string PrintName)
        {
            try
            {
                string ReadName = IniReadValue(key);
                if (!string.IsNullOrEmpty(ReadName))
                {
                    if (ReadName.Length > 0)
                    {
                        PrintName = ReadName;
                    }
                    else
                    {
                        var result = XtraInputBox.Show("请输入该单据打印机名称", $"指定【{key}】单据打印机配置", LocalPrinter.DefaultPrinter());
                        Write(key, result.ToString());
                        PrintName = result.ToString();
                    }
                }
                else
                {
                    var result = XtraInputBox.Show("请输入该单据打印机名称", $"指定【{key}】单据打印机配置", LocalPrinter.DefaultPrinter());
                    if (string.IsNullOrEmpty(result)) { result = LocalPrinter.DefaultPrinter(); XtraMessageBox.Show("未输入打印机名称,将设置成默认打印机打印该单据", "温馨提示"); }
                    Write(key, result.ToString());
                    PrintName = result.ToString();
                }
            }
            catch (Exception)
            {

                // throw;
            }
        }


    }

    #region MyRegion
    static class LocalPrinter
    {
        private static PrintDocument fPrintDocument = new PrintDocument();
        //获取本机默认打印机名称
        public static String DefaultPrinter()
        {
            return fPrintDocument.PrinterSettings.PrinterName;
        }
        public static List<String> GetLocalPrinters()
        {
            List<String> fPrinters = new List<String>();
            fPrinters.Add(DefaultPrinter()); //默认打印机始终出现在列表的第一项
            foreach (String fPrinterName in PrinterSettings.InstalledPrinters)
            {
                if (!fPrinters.Contains(fPrinterName))
                {
                    fPrinters.Add(fPrinterName);
                }
            }
            return fPrinters;
        }
    }
    #endregion
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值