diff --git a/OpcUaServiceManager/Form1.cs b/OpcUaServiceManager/Form1.cs index fca1c6bd..3cb95da1 100644 --- a/OpcUaServiceManager/Form1.cs +++ b/OpcUaServiceManager/Form1.cs @@ -19,8 +19,7 @@ public partial class Form1 : Form private ServiceController[] _winServices; private List _cogniteServices; private List _serviceNamesInUse; - private readonly string _opcuaExtractorDir; - private readonly string _opcuaExtractorExe = @"OpcUaExtractor\bin\OpcuaExtractor.exe"; + private readonly string _opcuaExtractorExe; private readonly bool _opcuaExtractorCanCreateService = false; private int _nextServiceNumber; @@ -32,27 +31,52 @@ public Form1() InitializeComponent(); // Set some default values - lblOpcUaExtractorFound.Text = "OpcUaService executeable not found."; - lblOpcUaExtractorFound.ForeColor = Color.Red; lblCmdRunStatus.Text = ""; lblServices.ForeColor = Color.Green; + lblCmdRunStatus.AutoSize = true; + lblOpcUaExtractorFound.AutoSize = true; + AutoSize = true; GetOpcUaExtractorServices(); // Get installdir from registry, created by the installer. - RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Cognite\OpcUaExtractor"); - if (key != null) - { - _opcuaExtractorDir = key.GetValue("InstallFolder").ToString(); - } + _opcuaExtractorExe = GetOpcUaExecutablePath(); - if (File.Exists(_opcuaExtractorDir + _opcuaExtractorExe)) + if (File.Exists(_opcuaExtractorExe)) { - lblOpcUaExtractorFound.Text = "OpcUaService found: " + _opcuaExtractorDir + _opcuaExtractorExe; + lblOpcUaExtractorFound.Text = $"OpcUaService found: {_opcuaExtractorExe}"; lblOpcUaExtractorFound.ForeColor = Color.Green; _opcuaExtractorCanCreateService = true; } + else + { + lblOpcUaExtractorFound.Text = $"OpcUaService executeable not found at {_opcuaExtractorExe}"; + lblOpcUaExtractorFound.ForeColor = Color.Red; + + } + } + + private string GetOpcUaExecutablePath() + { + // Priority 1, use a local file named opcuabinpath.txt + var localDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + + var cfgPath = Path.Combine(localDir, "opcuabinpath.txt"); + if (File.Exists(cfgPath)) + { + return File.ReadAllText(cfgPath).Trim(); + } + + // Priority 2, use the installation registry key (this is the default). + RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Cognite\OpcUaExtractor"); + if (key != null) + { + return Path.Combine(key.GetValue("InstallFolder").ToString(), @"OpcUaExtractor\bin\OpcuaExtractor.exe"); + } + + // Priorty 3, use a relative path + return Path.Combine(localDir, @"..\bin\OpcuaExtractor.exe"); } /// @@ -94,7 +118,7 @@ private void BtnSvcCreate_Click(object sender, EventArgs e) } string cmd = string.Format(@"/C sc create {2} binPath= ""\""{0}\"" -s -w \""{1}\"" "" DisplayName= ""{3}""", - _opcuaExtractorDir + _opcuaExtractorExe, txtSvcFolder.Text, _serviceBaseName + _nextServiceNumber, txtSvcName.Text); + _opcuaExtractorExe, txtSvcFolder.Text, _serviceBaseName + _nextServiceNumber, txtSvcName.Text); string result = RunCommand.Run(cmd); if (result.Contains("SUCCESS")) { @@ -105,7 +129,7 @@ private void BtnSvcCreate_Click(object sender, EventArgs e) } else { - lblCmdRunStatus.Text = "Failed to create service."; + lblCmdRunStatus.Text = $"Failed to create service: {result}"; lblCmdRunStatus.ForeColor = Color.Red; } @@ -156,7 +180,7 @@ private void BtnDeleteService_Click(object sender, EventArgs e) } else { - lblCmdRunStatus.Text = "Failed to delete service."; + lblCmdRunStatus.Text = $"Failed to delete service: {result}"; lblCmdRunStatus.ForeColor = Color.Red; } @@ -216,7 +240,8 @@ private void ListBoxOpcUaServices_SelectedIndexChanged(object sender, EventArgs if (!string.IsNullOrWhiteSpace(path)) { var pathItems = path.Split(' '); - path = (!string.IsNullOrWhiteSpace(pathItems[1])) ? pathItems[1].Replace(@"""", "") : ""; + var restPath = string.Concat(pathItems.Skip(3)); + path = (!string.IsNullOrWhiteSpace(restPath)) ? restPath.Replace(@"""", "") : ""; } txtSvcName.Text = service.DisplayName; diff --git a/manifest.yml b/manifest.yml index a57e3a63..8c242168 100644 --- a/manifest.yml +++ b/manifest.yml @@ -60,6 +60,14 @@ schema: - "https://raw.githubusercontent.com/" versions: + "2.13.1": + description: Minor improvements to the service manager + changelog: + added: + - "A file opcuabinpath.txt in the same folder as the service manager can be used to set the binary path" + - "If there is no opcuabinpath.txt and no installation registry entry, the service manager defaults to ../bin/OpcUaExtractor.exe" + fixed: + - "Display expected path if the service manager cannot find the executable" "2.13.0": description: Update to .NET 7 changelog: