locked
Add tag in .csproj at creation time of .csproj file RRS feed

  • Question

  • Hi,

      My problem is that i add <import> tag and also set property of <projectgroup> tag in .csproj file when it created.

    Another thing is that my existin solution contain multiple project(i.e. multiple .csproj file) then i want add <import> tag in existing .csproj programatically.

    I searched in most of forum but i can't get it properly so please solve my issues

    Thanks & Regards
    Satish Kumar

    • Changed type Chao Kuo Friday, May 7, 2010 8:26 AM Actually a question
    Tuesday, May 4, 2010 6:02 AM

Answers

  • Hello, Satish

    The Build Engine have a class called Project which has a Project.AddNewImport Method which enable you to add “Import” programmatically.

    But you need to unload your project first before you make some changes in .csproj, or a reload message will be prompted, because of the inconsistent between the file in disk and in memory.

    For how to unload a project, there are two method could do this.

    1.     IVsSolution2.CloseSolutionElement Method, IVsSolution2 is the service which you could get its object using GetService method.

    2.    Select the Project node in the solution explorer first and then use DTE.ExecuteCommand(“Project.UnloadProject”) to unload the project.

    a)     For how to select the project node programmatically, you could use UIHierarchyItem.Select() method to select the project Node, Please see this http://msdn.microsoft.com/en-us/library/envdte.uihierarchyitem.select(VS.80).aspx

    After unloading the project, add your import using  Project.AddNewImport Method.

    Then you need to programmatically reload the project, you need use UIHierarchyItem.Select Method to select the project you want to reload first, and then use DTE.ExecuteCommand("Project.ReloadProject", String.Empty).

    For instance

            Dim UIH As UIHierarchy = _

              DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object

            'Reload the WpfApplication2 project in the WpfApplication2 solution.

            Dim UIHItem As UIHierarchyItem = UIH.UIHierarchyItems.Item("WpfApplication2").UIHierarchyItems.Item("WpfApplication2")

     

            UIHItem.Select(vsUISelectionType.vsUISelectionTypeSetCaret)

            DTE.ExecuteCommand("Project.ReloadProject", String.Empty)

    Thanks

    Chao

    • Marked as answer by Chao Kuo Tuesday, May 11, 2010 2:33 AM
    Friday, May 7, 2010 8:24 AM

All replies

  • Hello, Satish

    The Build Engine have a class called Project which has a Project.AddNewImport Method which enable you to add “Import” programmatically.

    But you need to unload your project first before you make some changes in .csproj, or a reload message will be prompted, because of the inconsistent between the file in disk and in memory.

    For how to unload a project, there are two method could do this.

    1.     IVsSolution2.CloseSolutionElement Method, IVsSolution2 is the service which you could get its object using GetService method.

    2.    Select the Project node in the solution explorer first and then use DTE.ExecuteCommand(“Project.UnloadProject”) to unload the project.

    a)     For how to select the project node programmatically, you could use UIHierarchyItem.Select() method to select the project Node, Please see this http://msdn.microsoft.com/en-us/library/envdte.uihierarchyitem.select(VS.80).aspx

    After unloading the project, add your import using  Project.AddNewImport Method.

    Then you need to programmatically reload the project, you need use UIHierarchyItem.Select Method to select the project you want to reload first, and then use DTE.ExecuteCommand("Project.ReloadProject", String.Empty).

    For instance

            Dim UIH As UIHierarchy = _

              DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object

            'Reload the WpfApplication2 project in the WpfApplication2 solution.

            Dim UIHItem As UIHierarchyItem = UIH.UIHierarchyItems.Item("WpfApplication2").UIHierarchyItems.Item("WpfApplication2")

     

            UIHItem.Select(vsUISelectionType.vsUISelectionTypeSetCaret)

            DTE.ExecuteCommand("Project.ReloadProject", String.Empty)

    Thanks

    Chao

    • Marked as answer by Chao Kuo Tuesday, May 11, 2010 2:33 AM
    Friday, May 7, 2010 8:24 AM
  • Hello Satish,

    I'm not sure if this is what you were looking for, but it can't hurt.

    1. Get a reference to the current EnvDTE.Solution
    2. Get every instance of its "Projects" collection (through Projects.Item(i)).
    3. Check its Kind-property (use EnvDTE.Contants)
        - vsProjectKindSolutionItems indicates probable a solution-folder,  check each ProjectItem in the ProjectItems-property
        - vsProjectItemKindSolutionItems indicates that it is actually a valid Project-reference

    When checking the ProjectItems of a solution-folder the following values for Kind are of interest
    1. vsProjectItemKindSolutionItems (again) indicates a project. Get a valid reference through ProjectItem.SubProject property.
    2. vsProjectItemKind SubProject indicates the root for a subproject (if applicable in your case). Also get a valid reference through the SubProject-property.

    Once you have obtained a valid EnvDTE.Project reference you can cast it to VSLangProj80.VSProject2 like this

    VSLangProj80.VSProject2 _vsProject = (VSLangProj80.VSProject2)_project.Object;

    and with _vsProject.Imports.Add(string)

    You can add an import, also you can programmatically add references to other projects.

    See
    http://msdn.microsoft.com/en-us/library/vslangproj80.vsproject2_members(v=VS.80).aspx
    for members of VSLangProj80.VSProject2

    I hope this will help

     

    Sunday, May 9, 2010 10:17 AM
  • Hi, Rubin

    The OP want the import tag that is in Project MsBuild script. Like   <Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets" />

     And the VSProject2.Imports Property is used to reference the namespace of the project reference. For example, the import tag in VB.Net project, but the CSharp doesn’t have import, so it is always null in CSharp project.

    Thanks

    Chao

    Monday, May 10, 2010 3:10 AM