Skip to content

Commit

Permalink
Misc improvements to the service manager (#360)
Browse files Browse the repository at this point in the history
It was not entirely clear where it was trying to find the executable, this makes it clearer, and also adds some alternative options.
  • Loading branch information
einarmo authored Feb 16, 2023
1 parent 63e8055 commit 01ce51e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
55 changes: 40 additions & 15 deletions OpcUaServiceManager/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public partial class Form1 : Form
private ServiceController[] _winServices;
private List<ServiceController> _cogniteServices;
private List<string> _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;

Expand All @@ -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");
}

/// <summary>
Expand Down Expand Up @@ -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"))
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 01ce51e

Please sign in to comment.