同様の要件がありますが、残念ながらコマンド ライン パラメータをファイルに保存することはできませんでした。
免責事項: このアプローチは Windows でのみ有効です。
まず、インストール後のアクションを追加しました
x.AfterInstall(
installSettings =>
{
AddCommandLineParametersToStartupOptions(installSettings);
});
サービスの ImagePath Windowsレジストリ エントリAddCommanLineParameterToStartupOptions
を更新して、コマンド ライン パラメータを含めます。
TopShelf は、このステップの後にパラメーターを追加するので、重複を避けるため、servicename
これらinstance
を除外します。それら以外のものを除外したいかもしれませんが、私の場合はこれで十分でした.
private static void AddCommandLineParametersToStartupOptions(InstallHostSettings installSettings)
{
var serviceKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
$"SYSTEM\\CurrentControlSet\\Services\\{installSettings.ServiceName}",
true);
if (serviceKey == null)
{
throw new Exception($"Could not locate Registry Key for service '{installSettings.ServiceName}'");
}
var arguments = Environment.GetCommandLineArgs();
string programName = null;
StringBuilder argumentsList = new StringBuilder();
for (int i = 0; i < arguments.Length; i++)
{
if (i == 0)
{
// program name is the first argument
programName = arguments[i];
}
else
{
// Remove these servicename and instance arguments as TopShelf adds them as well
// Remove install switch
if (arguments[i].StartsWith("-servicename", StringComparison.InvariantCultureIgnoreCase) |
arguments[i].StartsWith("-instance", StringComparison.InvariantCultureIgnoreCase) |
arguments[i].StartsWith("install", StringComparison.InvariantCultureIgnoreCase))
{
continue;
}
argumentsList.Append(" ");
argumentsList.Append(arguments[i]);
}
}
// Apply the arguments to the ImagePath value under the service Registry key
var imageName = $"\"{Environment.CurrentDirectory}\\{programName}\" {argumentsList.ToString()}";
serviceKey.SetValue("ImagePath", imageName, Microsoft.Win32.RegistryValueKind.String);
}