aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication/MainStartup.cs
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2016-10-12 15:43:28 -0400
committerGitHub <noreply@github.com>2016-10-12 15:43:28 -0400
commit25ef9777cafee83c46ff53ede2caa04e3295e98a (patch)
tree5f1e6045d0b4d4d5b7d8dcaadf035b326f36e672 /MediaBrowser.ServerApplication/MainStartup.cs
parent0677d4ec990aee9a3bf7bda39dda01eb6fa66281 (diff)
parent5be6cf05e34459a046aceaa16c891f3034859476 (diff)
Merge pull request #2224 from MediaBrowser/beta
Beta
Diffstat (limited to 'MediaBrowser.ServerApplication/MainStartup.cs')
-rw-r--r--MediaBrowser.ServerApplication/MainStartup.cs98
1 files changed, 93 insertions, 5 deletions
diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs
index cdacdc81f..d1972a938 100644
--- a/MediaBrowser.ServerApplication/MainStartup.cs
+++ b/MediaBrowser.ServerApplication/MainStartup.cs
@@ -15,6 +15,7 @@ using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.ServiceProcess;
+using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -32,19 +33,47 @@ namespace MediaBrowser.ServerApplication
private static ILogger _logger;
private static bool _isRunningAsService = false;
+ private static bool _canRestartService = false;
private static bool _appHostDisposed;
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
+ public static bool TryGetLocalFromUncDirectory(string local, out string unc)
+ {
+ if ((local == null) || (local == ""))
+ {
+ unc = "";
+ throw new ArgumentNullException("local");
+ }
+
+ ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Name FROM Win32_share WHERE path ='" + local.Replace("\\", "\\\\") + "'");
+ ManagementObjectCollection coll = searcher.Get();
+ if (coll.Count == 1)
+ {
+ foreach (ManagementObject share in searcher.Get())
+ {
+ unc = share["Name"] as String;
+ unc = "\\\\" + SystemInformation.ComputerName + "\\" + unc;
+ return true;
+ }
+ }
+ unc = "";
+ return false;
+ }
/// <summary>
- /// Defines the entry point of the application.
- /// </summary>
+ /// Defines the entry point of the application.
+ /// </summary>
public static void Main()
{
var options = new StartupOptions();
_isRunningAsService = options.ContainsOption("-service");
+ if (_isRunningAsService)
+ {
+ _canRestartService = CanRestartWindowsService();
+ }
+
var currentProcess = Process.GetCurrentProcess();
var applicationPath = currentProcess.MainModule.FileName;
@@ -238,7 +267,14 @@ namespace MediaBrowser.ServerApplication
{
get
{
- return !_isRunningAsService;
+ if (_isRunningAsService)
+ {
+ return _canRestartService;
+ }
+ else
+ {
+ return true;
+ }
}
}
@@ -250,7 +286,14 @@ namespace MediaBrowser.ServerApplication
{
get
{
- return !_isRunningAsService;
+ if (_isRunningAsService)
+ {
+ return _canRestartService;
+ }
+ else
+ {
+ return true;
+ }
}
}
@@ -591,7 +634,11 @@ namespace MediaBrowser.ServerApplication
{
DisposeAppHost();
- if (!_isRunningAsService)
+ if (_isRunningAsService)
+ {
+ RestartWindowsService();
+ }
+ else
{
//_logger.Info("Hiding server notify icon");
//_serverNotifyIcon.Visible = false;
@@ -646,6 +693,47 @@ namespace MediaBrowser.ServerApplication
}
}
+ private static void RestartWindowsService()
+ {
+ _logger.Info("Restarting background service");
+
+ var startInfo = new ProcessStartInfo
+ {
+ FileName = "cmd.exe",
+ CreateNoWindow = true,
+ WindowStyle = ProcessWindowStyle.Hidden,
+ Verb = "runas",
+ ErrorDialog = false,
+ Arguments = String.Format("/c sc stop {0} & sc start {0} & sc start {0}", BackgroundService.GetExistingServiceName())
+ };
+ Process.Start(startInfo);
+ }
+
+ private static bool CanRestartWindowsService()
+ {
+ var startInfo = new ProcessStartInfo
+ {
+ FileName = "cmd.exe",
+ CreateNoWindow = true,
+ WindowStyle = ProcessWindowStyle.Hidden,
+ Verb = "runas",
+ ErrorDialog = false,
+ Arguments = String.Format("/c sc query {0}", BackgroundService.GetExistingServiceName())
+ };
+ using (var process = Process.Start(startInfo))
+ {
+ process.WaitForExit();
+ if (process.ExitCode == 0)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+
private static async Task InstallVcredist2013IfNeeded(ApplicationHost appHost, ILogger logger)
{
// Reference