示例#1
0
        public HierarchyNode AddItem(string name)
        {
            Guard.ArgumentNotNullOrEmptyString(name, "name");
            if (!CanAddItem(name))
            {
                throw new InvalidOperationException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              Resources.InvalidFileName,
                              name));
            }
            FileInfo fileInfo;
            string   subFolder = string.Empty;

            if (System.IO.Path.IsPathRooted(name))
            {
                fileInfo = new FileInfo(name);
            }
            else
            {
                fileInfo = new FileInfo(System.IO.Path.Combine(ProjectDir, name));
                int subFolderIndex = name.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
                if (subFolderIndex != -1)
                {
                    subFolder = name.Substring(0, subFolderIndex);
                }
            }
            if (fileInfo.Name.Equals(fileInfo.Extension, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              Resources.CannotCreateItemWithEmptyName));
            }
            if (!File.Exists(fileInfo.FullName))
            {
                Directory.CreateDirectory(fileInfo.Directory.FullName);
                File.Create(fileInfo.FullName).Dispose();
            }
            VSDOCUMENTPRIORITY docPri = VSDOCUMENTPRIORITY.DP_Standard;
            int hr = _project.IsDocumentInProject(fileInfo.FullName, out int found, new[] { docPri }, out uint itemId);

            Marshal.ThrowExceptionForHR(hr);
            if (found == 0)
            {
                VSADDRESULT   result        = VSADDRESULT.ADDRESULT_Cancel;
                uint          folderId      = ItemId;
                HierarchyNode subFolderNode = FindSubfolder(subFolder);
                if (subFolderNode != null)
                {
                    folderId = subFolderNode.ItemId;
                }
                hr = _project.AddItem(folderId,
                                      VSADDITEMOPERATION.VSADDITEMOP_OPENFILE,
                                      fileInfo.Name, 1, new[] { fileInfo.FullName },
                                      IntPtr.Zero, new[] { result });
                Marshal.ThrowExceptionForHR(hr);
            }
            hr = _project.IsDocumentInProject(fileInfo.FullName, out found, new[] { docPri }, out itemId);
            Marshal.ThrowExceptionForHR(hr);
            if (found == 1)
            {
                return(new HierarchyNode(this, itemId));
            }
            return(null);
        }
示例#2
0
 private ProjectNode(HierarchyNode parent, uint itemid)
     : base(parent, itemid)
 {
     _project = Hierarchy as IVsProject;
     Debug.Assert(_project != null);
 }
示例#3
0
 public HierarchyNodeCollection(HierarchyNode parent)
 {
     Debug.Assert(parent != null);
     _parent = parent;
 }