internal static void AddNestedFile(DTE2 dte, string parent, string child, BuildActionType type) { // ignore if child doesn't exist if (!File.Exists(child)) return; if (dte == null) return; // if we can't load parent or child already part of solution, don't attempt to change anything ProjectItem parentItem, childItem; if (!TryGetProjectItem(dte.Solution, parent, out parentItem) || TryGetProjectItem(dte.Solution, child, out childItem)) return; // add the child item and save project if (parentItem.ProjectItems == null) Logger.Log("ProjectItems is null. Bad news is about to happen."); childItem = parentItem.ProjectItems.AddFromFile(child); if (childItem != null) { childItem.ContainingProject.Save(); } else { Logger.Log("Could not add child item to project."); } // schedule call to change build action // this is a work around since it seems to ignore property changes until after file saved // and even after that it still ignores it, so async makes it better Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => ChangeBuildActionType(dte, child, type)), DispatcherPriority.Background); }
private static void ChangeBuildActionType(DTE2 dte, string path, BuildActionType type) { ProjectItem item; if (TryGetProjectItem(dte.Solution, path, out item)) { var vsBuildAction = (int)type; var vsType = type.ToString(); var actionProperty = item.Properties.Item("BuildAction"); var typeProperty = item.Properties.Item("ItemType"); // stop if no changes if (vsBuildAction.Equals(actionProperty.Value) && vsType.Equals(typeProperty.Value)) return; actionProperty.Value = vsBuildAction; typeProperty.Value = vsType; item.ContainingProject.Save(); } }