Update the files related to settings.ini
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
Namhyeon Go 2024-08-13 13:39:01 +09:00
parent d04e03200b
commit f44f0b38b2
3 changed files with 89 additions and 7 deletions

View File

@ -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))
{

View File

@ -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;
}
}
}

View File

@ -144,6 +144,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScreenMatching.cs" />
<Compile Include="ScreenMatchResult.cs" />
<Compile Include="TinyINIController\IniFile.cs" />
</ItemGroup>
<ItemGroup>
<COMReference Include="MSScriptControl">