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
}