using OpenVinoSharp;
|
using SharpCompress.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Reflection.Metadata;
|
using System.Runtime.InteropServices;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
using static System.Collections.Specialized.BitVector32;
|
|
namespace LB_SmartVision.Tool
|
{
|
public class OperateIniHelper
|
{
|
private string _iniFilePath = "Config";
|
|
/// <summary>
|
/// IniFilePath需要包含.ini,在引用前需要设置FileName
|
/// </summary>
|
public string IniFilePath
|
{
|
get
|
{
|
return System.IO.Path.Combine(Application.StartupPath, _iniFilePath, _iniFileName);
|
}
|
set
|
{
|
_iniFilePath = value;
|
}
|
}
|
|
private string _iniFileName = "Default.ini";
|
|
public static string Suffix = ".ini";
|
|
private bool bClosedIni = false;
|
public string IniFileName
|
{
|
get
|
{
|
return _iniFileName;
|
}
|
set
|
{
|
int index = value.LastIndexOf('.');
|
|
// 如果没有找到分隔符,则返回原始字符串
|
if (index == -1)
|
_iniFileName = value + Suffix;
|
else
|
_iniFileName = value.Substring(0, index) + Suffix;
|
}
|
}
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
|
private static extern int WritePrivateProfileString(string section, string key, string value, string filePath);
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
|
private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder returnValue, int size, string filePath);
|
|
public OperateIniHelper() { }
|
|
public OperateIniHelper(string iniFileName = "MyAlgorithm_1", string iniFilePath = "MyAlgorithm")
|
{
|
IniFileName = iniFileName;
|
IniFilePath = iniFilePath;
|
GetValidFilePath(IniFilePath);
|
}
|
|
public void SetValue(string section, string key, object value, string path = "")
|
{
|
// 选择合适的编码
|
Encoding encoding = Encoding.UTF8;
|
if (path != "" && value != null)
|
{
|
WritePrivateProfileString(section.Trim(), key.Trim(), value.ToString(), path);
|
int index = path.LastIndexOf('.');
|
if (index != -1)
|
{
|
string EncryptPath = path.Substring(0, index) + "_closed" + Suffix;
|
Tool.EncryptFile(path, EncryptPath);
|
}
|
}
|
else if (value != null)
|
{
|
WritePrivateProfileString(section.Trim(), key.Trim(), value.ToString(), IniFilePath);
|
int index = IniFilePath.LastIndexOf('.');
|
if (index != -1)
|
{
|
string EncryptPath = IniFilePath.Substring(0, index) + "_closed" + Suffix;
|
Tool.EncryptFile(IniFilePath, EncryptPath);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 获取Ini文件值
|
/// </summary>
|
/// <param name="section"></param>
|
/// <param name="key"></param>
|
/// <param name="path"></param>
|
/// <returns></returns>
|
public string GetValue(string section, string key, string path = "")
|
{
|
StringBuilder returnValue = new StringBuilder(255);
|
|
if (path != "" && path != null && GetValidFilePath(path).Item1)
|
GetPrivateProfileString(section.Trim(), key.Trim(), string.Empty, returnValue, returnValue.Capacity, path);
|
|
else if (GetValidFilePath(IniFilePath).Item1)
|
GetPrivateProfileString(section.Trim(), key.Trim(), string.Empty, returnValue, returnValue.Capacity, IniFilePath);
|
|
//不存在值时写入默认值0
|
if (string.IsNullOrEmpty(returnValue.ToString().Trim()))
|
{
|
string strDefalut = "0";
|
if (path != "" && path != null)
|
SetValue(section.Trim(), key.Trim(), strDefalut, path);
|
else
|
SetValue(section.Trim(), key.Trim(), strDefalut);
|
|
return strDefalut;
|
}
|
|
return returnValue.ToString();
|
}
|
|
static (bool, string) GetValidFilePath(string path)
|
{
|
try
|
{
|
if (path == "" || path == null)
|
return (false, null);
|
|
// 检查路径是否包含非法字符
|
string fullPath = Path.GetFullPath(path);
|
bool result = !string.IsNullOrWhiteSpace(fullPath) && path.Equals(fullPath, StringComparison.OrdinalIgnoreCase);
|
|
// 不存在非法字符则判断是否需要创建
|
if (result)
|
{
|
string parentPath = Path.GetDirectoryName(path);
|
// 判断文件夹是否存在
|
if (!Directory.Exists(parentPath) && parentPath != null)
|
{
|
try
|
{
|
Directory.CreateDirectory(parentPath);
|
// 文件不存在则写入默认值
|
}
|
catch { return (false, null); }
|
}
|
|
}
|
return (result, path);
|
}
|
catch { return (false, null); }
|
}
|
}
|
}
|