轮胎外观检测添加思谋语义分割模型检测工具
C3204
16 小时以前 848ae3a7841a16322c23f9c0d10b2fab83a97c2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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); }
        }
    }
}