locked
How to get the object of the file present in the project in c#? RRS feed

  • Question

  • Hi All,

    This is related to extensibility. I want to get the object of the file located in my custom project. when file is opened in the editor i am able to get the object but none of the file is open in the editor then i am not able to get the object.

    Please help me.

    Regards,

    abhishek

     filePath = Path.ChangeExtension(filePath, BackendConstants.LanguageFormat.UsingLocalExtension(Path.GetExtension(filePath)).getFileExt());
                CustomFileNode = null;
                Document doc;
                ProjectItem item;
                Type docType;
                try
                {
                    DTE2 dte2 = ServiceProvider.GlobalProvider.GetService(typeof(SDTE)) as DTE2;
                    Documents documents = dte2.Documents;
                    
                    int count = documents.Count;
                    for (int i = 1; i <= count; i++)
                    {
                        doc = documents.Item(i);
                        
                        if (doc.ActiveWindow != null)
                        {
                            item = doc.ProjectItem;
                            docType = item.Object.GetType();
                            if (typeof(CustomFileNode) == docType)
                            {
                                CustomFileNode = (CustomFileNode)item.Object;
                                ((CustomProjectNode)node.projectNode).PathChange = true;
                                if (node.Url.Equals(filePath))
                                {
                                    fileNode = node;
                                    ((CustomProjectNode)node.projectNode).PathChange = false;
                                    break;
                                }
                                ((CustomProjectNode)node.projectNode).PathChange = false;
    
                            }
                        }
                    }
    
                    return fileNode;
                }


    • Edited by abhi0022 Wednesday, November 16, 2016 10:22 AM
    • Moved by CoolDadTx Wednesday, November 16, 2016 4:03 PM Extensibility related
    Wednesday, November 16, 2016 10:21 AM

All replies

  • Hi abhi0022,

    According to your description, it seems that you want to retrieve object of the file in the project, if it is the case, you could use project class to achieve it. like this:

    var p = new Project(@"D:\Project\Msbuild\CustomTaskDemo\Test\Test.csproj");
    
    foreach (var item in p.Items)
    {
        Console.WriteLine(item.EvaluatedInclude);
    }
    Console.ReadKey();

    For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/microsoft.build.evaluation.project.aspx

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 17, 2016 6:18 AM