locked
How can i programmatically add/change Import tag to project file RRS feed

  • Question

  • I'm trying to create project template.
    when i see in project file(.csproj) created by project templte  there is absolute path created for project attribute in Import tag i want this to be relative path how can i do that

    I tried to parse the project file and change project value to relative path (i'm doing this in  IWizard.ProjectFinishedGenerating) but after loading the project it is again changing to absolute path

    Sample of Import tag in my project file(i want below path to be relative path)

    <

     

    Import Project="C:\Code\ProjectFolder1\ProjectFolder2\CodeAnalysis.Errors.targets" />

    I'm using VS 2008


    Friday, August 14, 2009 1:01 PM

Answers

  • Hi,
    Thanks for your following up, now I finally find a solution for you.

    For the sequence of the events is RunStarted->ProjectFinishedGenerating->RunFinished. So I moved the statement to the RunFinished method.The code below will show you the code I wrote for you. And please pay special attention to my comment in the Runfinished() method. Hope this code could resolve your problem.
     #region IWizard Members
            Engine engine;
            string fileName;
            DTE dte;
            Microsoft.Build.BuildEngine.Project proj;
            void IWizard.BeforeOpeningFile(ProjectItem projectItem)
            {
                
            }
    
            void IWizard.ProjectFinishedGenerating(EnvDTE.Project project)
            {
                fileName = project.FullName;
                
               
            }
    
            void IWizard.ProjectItemFinishedGenerating(ProjectItem projectItem)
            {
              
                
                
            }
    
            void IWizard.RunFinished()
            {
                proj.Load(fileName);
                //Because modify the open *.csproj file outside of the IDE will pop up a window to let you reload the file.
                //I Execute Command to unload the project before modifying the *.csproj file. And reload after the *.csproj has already updated.
                dte.ExecuteCommand("File.SaveAll", string.Empty);
                dte.ExecuteCommand("Project.UnloadProject", string.Empty);
                proj.Imports.AddNewImport(@"C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.CSharp.targets", null);
                proj.Save(fileName);
                dte.ExecuteCommand("Project.ReloadProject", string.Empty);
            }
    
            void IWizard.RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
            {
                dte = automationObject as DTE;
                engine = new Engine();
                proj = new Microsoft.Build.BuildEngine.Project(engine);
            }
    
            bool IWizard.ShouldAddProjectItem(string filePath)
            {
    
                return true;
            }
    
            #endregion
    Thanks
    Kaku
    • Edited by The old KAKU Thursday, August 20, 2009 2:00 AM
    • Marked as answer by NagaVital Thursday, August 20, 2009 3:40 PM
    Wednesday, August 19, 2009 10:32 AM

All replies

  • Hi NagaVital,

    If you want to add/change Import tag to project file, you can use the Microsoft.Build.BuildEngine.Project. See Microsoft.Build.Buildengine.Project for more information. I have writtem some sample code for you.
       void IWizard.ProjectFinishedGenerating(EnvDTE.Project project)
            {
                Engine engine = new Engine();
                Microsoft.Build.BuildEngine.Project proj = new Microsoft.Build.BuildEngine.Project(engine);
                proj.FullFileName = project.FullName;
                proj.Imports.AddNewImport(@"$(MSBuildToolsPath)\Microsoft.WinFx.targets", null);
            }
    You mentioned you want the Import tag to be relative path. Based on my understanding I am afraid it is not supported,you can refer the link http://msdn.microsoft.com/en-us/library/92x05xfs.aspx, and notice the sentence -- All relative paths in imported projects are interpreted relative to the directory of the imported project...
    If I misunderstood you ,please feel free to let me know.
    Kaku
    • Proposed as answer by Matthew Church Wednesday, October 15, 2014 9:02 PM
    Monday, August 17, 2009 4:43 AM
  • Hi,

    Thanks a lot for your detail reply.

    I tried to implement as per your sample code but it is not updating new import tag to the project so i have done changes as below

    void IWizard.ProjectFinishedGenerating(EnvDTE.Project project)
    {
                Engine engine = new Engine();
                Microsoft.Build.BuildEngine.Project proj = new Microsoft.Build.BuildEngine.Project(engine);
                proj.Load(project.FullName)
                proj.Imports.AddNewImport(@"$(MSBuildToolsPath)\Microsoft.WinFx.targets", null);
                proj.Save(project.FullName)
    }

    with above code
    In the solution it is showing a window "Conflicting File modification detected" which is having 4 buttons "Save as", "Discard", "Overwrite" and "Ignore"

    when i press "Discard" we are able see newly added import tag if we press overwrite I'm unable to see the new import tag added

    I dont want to display this window when we add project even if it is displayed user will generally click "Overwrite"  but not ignore, how can i do that

    I guess after ProjectFinishedGenerating event is fired somewhere projectfile is being saved, we should add the import tag after project is saved


    -Vital

    Tuesday, August 18, 2009 3:48 PM
  • Hi,
    Thanks for your following up, now I finally find a solution for you.

    For the sequence of the events is RunStarted->ProjectFinishedGenerating->RunFinished. So I moved the statement to the RunFinished method.The code below will show you the code I wrote for you. And please pay special attention to my comment in the Runfinished() method. Hope this code could resolve your problem.
     #region IWizard Members
            Engine engine;
            string fileName;
            DTE dte;
            Microsoft.Build.BuildEngine.Project proj;
            void IWizard.BeforeOpeningFile(ProjectItem projectItem)
            {
                
            }
    
            void IWizard.ProjectFinishedGenerating(EnvDTE.Project project)
            {
                fileName = project.FullName;
                
               
            }
    
            void IWizard.ProjectItemFinishedGenerating(ProjectItem projectItem)
            {
              
                
                
            }
    
            void IWizard.RunFinished()
            {
                proj.Load(fileName);
                //Because modify the open *.csproj file outside of the IDE will pop up a window to let you reload the file.
                //I Execute Command to unload the project before modifying the *.csproj file. And reload after the *.csproj has already updated.
                dte.ExecuteCommand("File.SaveAll", string.Empty);
                dte.ExecuteCommand("Project.UnloadProject", string.Empty);
                proj.Imports.AddNewImport(@"C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.CSharp.targets", null);
                proj.Save(fileName);
                dte.ExecuteCommand("Project.ReloadProject", string.Empty);
            }
    
            void IWizard.RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
            {
                dte = automationObject as DTE;
                engine = new Engine();
                proj = new Microsoft.Build.BuildEngine.Project(engine);
            }
    
            bool IWizard.ShouldAddProjectItem(string filePath)
            {
    
                return true;
            }
    
            #endregion
    Thanks
    Kaku
    • Edited by The old KAKU Thursday, August 20, 2009 2:00 AM
    • Marked as answer by NagaVital Thursday, August 20, 2009 3:40 PM
    Wednesday, August 19, 2009 10:32 AM
  • Hi,

    Great it is working fine, thanks for your help


    -Vital

    • Edited by NagaVital Monday, September 7, 2009 10:34 AM
    Thursday, August 20, 2009 3:59 PM