Microsoft.VisualStudio.Project.ProjectNode.GetMkDocument C# (CSharp) Method

GetMkDocument() public method

Callback from the additem dialog. Deals with adding new and existing items
public GetMkDocument ( uint itemId, string &mkDoc ) : int
itemId uint
mkDoc string
return int
        public virtual int GetMkDocument(uint itemId, out string mkDoc)
        {
            mkDoc = null;
            if (itemId == VSConstants.VSITEMID_SELECTION)
            {
                return VSConstants.E_UNEXPECTED;
            }

            HierarchyNode n = this.NodeFromItemId(itemId);
            if (n == null)
            {
                return VSConstants.E_INVALIDARG;
            }

            mkDoc = n.GetMkDocument();

            if (String.IsNullOrEmpty(mkDoc))
            {
                return VSConstants.E_FAIL;
            }

            return VSConstants.S_OK;
        }

Same methods

ProjectNode::GetMkDocument ( ) : string

Usage Example

        public string GetFileNamespace(string fileFullPath, ProjectNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // Get base namespace from the project
            string namespce = node.GetProjectProperty("RootNamespace");
            if(String.IsNullOrEmpty(namespce))
                namespce = Path.GetFileNameWithoutExtension(fileFullPath); ;

            // If the item is added to a subfolder, the name space should reflect this.
            // This is done so that class names from 2 files with the same name but different
            // directories don't conflict.
            string relativePath = Path.GetDirectoryName(fileFullPath);
            string projectPath = Path.GetDirectoryName(node.GetMkDocument());
            // Our project system only support adding files that are sibling of the project file or that are in subdirectories.
            if(String.Compare(projectPath, 0, relativePath, 0, projectPath.Length, true, CultureInfo.CurrentCulture) == 0)
            {
                relativePath = relativePath.Substring(projectPath.Length);
            }
            else
            {
                Debug.Fail("Adding an item to the project that is NOT under the project folder.");
                // We are going to use the full file path for generating the namespace
            }

            // Get the list of parts
            int index = 0;
            string[] pathParts;
            pathParts = relativePath.Split(Path.DirectorySeparatorChar);

            // Use a string builder with default size being the expected size
            StringBuilder result = new StringBuilder(namespce, namespce.Length + relativePath.Length + 1);
            // For each path part
            while(index < pathParts.Length)
            {
                string part = pathParts[index];
                ++index;

                // This could happen if the path had leading/trailing slash, we want to ignore empty pieces
                if(String.IsNullOrEmpty(part))
                    continue;

                // If we reach here, we will be adding something, so add a namespace separator '.'
                result.Append('.');

                // Make sure it starts with a letter
                if(!char.IsLetter(part, 0))
                    result.Append('N');

                // Filter invalid namespace characters
                foreach(char c in part)
                {
                    if(char.IsLetterOrDigit(c))
                        result.Append(c);
                }
            }
            return result.ToString();
        }
All Usage Examples Of Microsoft.VisualStudio.Project.ProjectNode::GetMkDocument
ProjectNode