Merge pull request #77 from ebsco/configfilesearchfix3

Fix null exception and throw a FileNotFoundException if xml file is neve...
pull/90/head
Oleg Nenashev 2015-02-01 15:55:09 +03:00
commit d430642769
1 changed files with 9 additions and 3 deletions

View File

@ -45,18 +45,24 @@ namespace winsw
{ {
// find co-located configuration xml. We search up to the ancestor directories to simplify debugging, // find co-located configuration xml. We search up to the ancestor directories to simplify debugging,
// as well as trimming off ".vshost" suffix (which is used during debugging) // as well as trimming off ".vshost" suffix (which is used during debugging)
//Get the first parent to go into the recursive loop
string p = ExecutablePath; string p = ExecutablePath;
string baseName = Path.GetFileNameWithoutExtension(p); string baseName = Path.GetFileNameWithoutExtension(p);
if (baseName.EndsWith(".vshost")) baseName = baseName.Substring(0, baseName.Length - 7); if (baseName.EndsWith(".vshost")) baseName = baseName.Substring(0, baseName.Length - 7);
DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(p));
while (true) while (true)
{ {
p = Path.GetDirectoryName(p); if (File.Exists(Path.Combine(d.FullName, baseName + ".xml")))
if (File.Exists(Path.Combine(p, baseName + ".xml")))
break; break;
if (d.Parent == null)
throw new FileNotFoundException("Unable to locate "+baseName+".xml file within executable directory or any parents");
d = d.Parent;
} }
BaseName = baseName; BaseName = baseName;
BasePath = Path.Combine(p, BaseName); BasePath = Path.Combine(d.FullName, BaseName);
dom.Load(BasePath + ".xml"); dom.Load(BasePath + ".xml");