private bool TryFindParentFileNode(HierarchyNode root, string child, out HierarchyNode parent)
{
parent = null;
var childName = Path.GetFileName(child);
var childDirectory = Path.GetDirectoryName(child);
// the standart layout, used for most file types (aspx, ashx, master, xaml etc)
// + - page.aspx
// | + - page.aspx.n
// | + - page.aspx.designer.n
var parentName = Path.GetFileNameWithoutExtension(childName);
while (parentName.IndexOf('.') > 0 && parent == null)
{
parent = root.FindChild(Path.Combine(childDirectory, parentName));
parentName = Path.GetFileNameWithoutExtension(parentName);
}
if (parent == null)
{
// Windows forms layout:
// + - form.n
// | + - form.designer.n
var childNameWithoutExtension = Path.GetFileNameWithoutExtension(child);
// look for suffix position (".Designer", etc)
var suffixIndex = childNameWithoutExtension.LastIndexOf(root.NameRelationSeparator);
if (suffixIndex < 0)
return false;
parentName = string.Format("{0}.n", childNameWithoutExtension.Substring(0, suffixIndex));
var parentPath = Path.Combine(childDirectory, parentName);
parent = root.FindChild(parentPath);
}
return parent != null;
}