Browse Source

change the wrapped process working directory

set the working directory of the wrapped process with the location of the winsw executable, or the path defined in the configuration file.
teddy 12 years ago
parent
commit
39891488bf
2 changed files with 15 additions and 3 deletions
  1. 1 0
      Main.cs
  2. 14 3
      ServiceDescriptor.cs

+ 1 - 0
Main.cs

@@ -373,6 +373,7 @@ namespace winsw
             var ps = process.StartInfo;
             ps.FileName = executable;
             ps.Arguments = arguments;
+            ps.WorkingDirectory = descriptor.WorkingDirectory;
             ps.CreateNoWindow = false;
             ps.UseShellExecute = false;
             ps.RedirectStandardInput = true; // this creates a pipe for stdin to the new process, instead of having it inherit our stdin.

+ 14 - 3
ServiceDescriptor.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Diagnostics;
+using System.Reflection;
 using System.Runtime.InteropServices;
 using System.ServiceProcess;
 using System.Text;
@@ -69,11 +70,11 @@ namespace winsw
             dom.Load(BasePath + ".xml");
         }
 
-        private string SingleElement(string tagName)
+        private string SingleElement(string tagName, bool optional = false)
         {
             var n = dom.SelectSingleNode("//" + tagName);
-            if (n == null) throw new InvalidDataException("<" + tagName + "> is missing in configuration XML");
-            return Environment.ExpandEnvironmentVariables(n.InnerText);
+            if (n == null && !optional) throw new InvalidDataException("<" + tagName + "> is missing in configuration XML");
+            return n == null ? null : Environment.ExpandEnvironmentVariables(n.InnerText);
         }
 
         private int SingleIntElement(XmlNode parent, string tagName, int defaultValue)
@@ -169,6 +170,16 @@ namespace winsw
             }
         }
 
+        /// <summary>
+        /// Optional working directory.
+        /// </summary>
+        public string WorkingDirectory {
+            get {
+                var wd = SingleElement("workingdirectory", true);
+                return String.IsNullOrEmpty(wd) ? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) : wd;
+            }
+        }
+
         /// <summary>
         /// Combines the contents of all the elements of the given name,
         /// or return null if no element exists. Handles whitespace quotation.