-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
80 lines (68 loc) · 2.76 KB
/
Program.cs
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
using System;
using System.IO;
using System.Runtime.InteropServices;
using Newtonsoft.Json.Linq;
class Program
{
static void Main()
{
Console.SetWindowSize(34, 13);
Console.SetBufferSize(34, 13);
Console.Clear();
Console.Title = "Check HMI Version";
string jsonFile = File.ReadAllText("HMI_LIST.json");
JObject jsonObj = JObject.Parse(jsonFile);
JArray hmiList = (JArray)jsonObj["hmi"];
Console.WriteLine("{0, 33}", "HMI Name & IP | Version");
foreach (JObject sectionObj in hmiList)
{
JObject section = (JObject)sectionObj["section"];
string sectionName = (string)section["name"];
Console.WriteLine(sectionName + " :");
JArray data = (JArray)section["data"];
foreach (JObject hmiObj in data)
{
string hmiName = (string)hmiObj["hmi_name"];
string hmiIp = (string)hmiObj["hmi_ip"];
string iniFilePath = (string)hmiObj["ini_file_path"];
string iniFileFullPath = "\\\\" + hmiIp + iniFilePath;
Console.WriteLine("{0, 7} - {1, -13} : {2, 7}", hmiName, hmiIp, GetHMIVersion(iniFileFullPath));
}
Console.WriteLine();
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
static string GetHMIVersion(string filePath)
{
if (!File.Exists(filePath)) return "ERROR"; // The file path does not exist
string section = "InTouch";
string key = "ApplicationVersionNo";
var iniData = new Dictionary<string, Dictionary<string, string>>();
string currentSection = null;
foreach (string line in File.ReadLines(filePath))
{
string trimmedLine = line.Trim();
if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]"))
{
currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2);
iniData[currentSection] = new Dictionary<string, string>();
}
else if (!string.IsNullOrEmpty(trimmedLine) && currentSection != null)
{
int equalsIndex = trimmedLine.IndexOf('=');
if (equalsIndex >= 0)
{
string iniKey = trimmedLine.Substring(0, equalsIndex).Trim();
string iniValue = trimmedLine.Substring(equalsIndex + 1).Trim();
iniData[currentSection][iniKey] = iniValue;
}
}
}
if (iniData.ContainsKey(section) && iniData[section].ContainsKey(key))
{
return iniData[section][key];
}
return "N/A";
}
}