From f44f0b38b2932ae8424e42b3c2b8f571c5f7eca8 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Tue, 13 Aug 2024 13:39:01 +0900 Subject: [PATCH] Update the files related to settings.ini --- .../WelsonJS.Service/ServiceMain.cs | 23 ++++-- .../TinyINIController/IniFile.cs | 72 +++++++++++++++++++ .../WelsonJS.Service/WelsonJS.Service.csproj | 1 + 3 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 WelsonJS.Toolkit/WelsonJS.Service/TinyINIController/IniFile.cs diff --git a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs index 9994839..bb94bb0 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs @@ -31,7 +31,7 @@ using System.Runtime.InteropServices; using MSScriptControl; using System.IO; using System.Collections.Generic; -using System.Text; +using WelsonJS.Service.TinyINIController; namespace WelsonJS.Service { @@ -50,16 +50,11 @@ namespace WelsonJS.Service private bool disabledFileMonitor = false; private ScreenMatching screenMatcher; private FileEventMonitor fileEventMonitor; + private IniFile settingsController; [DllImport("user32.dll")] private static extern int GetSystemMetrics(int nIndex); - [DllImport("kernel32.dll")] - private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); - - [DllImport("kernel32.dll")] - private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); - private static int SM_REMOTESESSION = 0x1000; public ServiceMain(string[] args) @@ -110,6 +105,20 @@ namespace WelsonJS.Service } Directory.SetCurrentDirectory(workingDirectory); + // read settings.ini + string settingsFilePath = Path.Combine(workingDirectory, "settings.ini"); + if (File.Exists(settingsFilePath)) + { + try + { + settingsController = new IniFile(settingsFilePath); + } + catch (Exception) + { + settingsController = null; + } + } + // set script name if (string.IsNullOrEmpty(scriptName)) { diff --git a/WelsonJS.Toolkit/WelsonJS.Service/TinyINIController/IniFile.cs b/WelsonJS.Toolkit/WelsonJS.Service/TinyINIController/IniFile.cs new file mode 100644 index 0000000..4032fd9 --- /dev/null +++ b/WelsonJS.Toolkit/WelsonJS.Service/TinyINIController/IniFile.cs @@ -0,0 +1,72 @@ +using System; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Text; + +// https://github.com/niklyadov/tiny-ini-file-class +namespace WelsonJS.Service.TinyINIController +{ + class IniFile + { + [DllImport("kernel32", CharSet = CharSet.Unicode)] + static extern long WritePrivateProfileString(string section, string key, string value, string FilePath); + + [DllImport("kernel32", CharSet = CharSet.Unicode)] + static extern int GetPrivateProfileString(string section, string key, string Default, StringBuilder RetVal, int Size, string FilePath); + + private readonly FileInfo FileInfo; + + private readonly string exe = Assembly.GetExecutingAssembly().GetName().Name; + + private readonly FileAccess fileAccess; + + public IniFile(string path = null, FileAccess access = FileAccess.ReadWrite) + { + fileAccess = access; + FileInfo = new FileInfo(path ?? exe); + } + + public string Read(string key, string section = null) + { + var RetVal = new StringBuilder(65025); + + if (fileAccess != FileAccess.Write) + { + GetPrivateProfileString(section ?? exe, key, "", RetVal, 65025, FileInfo.FullName); + } + else + { + throw new Exception("Can`t read file! No access!"); + } + + return RetVal.ToString(); + } + public void Write(string key, string value, string section = null) + { + if (fileAccess != FileAccess.Read) + { + WritePrivateProfileString(section ?? exe, key, value, FileInfo.FullName); + } + else + { + throw new Exception("Can`t write to file! No access!"); + } + } + + public void DeleteKey(string key, string section = null) + { + Write(key, null, section ?? exe); + } + + public void DeleteSection(string section = null) + { + Write(null, null, section ?? exe); + } + + public bool KeyExists(string key, string section = null) + { + return Read(key, section).Length > 0; + } + } +} diff --git a/WelsonJS.Toolkit/WelsonJS.Service/WelsonJS.Service.csproj b/WelsonJS.Toolkit/WelsonJS.Service/WelsonJS.Service.csproj index d80200e..6b33a33 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/WelsonJS.Service.csproj +++ b/WelsonJS.Toolkit/WelsonJS.Service/WelsonJS.Service.csproj @@ -144,6 +144,7 @@ +