locked
After calling Project.ProjectItems.AddFromFile, cant access the newly added file. RRS feed

  • Question

  • Hello.

    I am trying to add a new file to a project from a visual studio package project. In this vs package I am trying to add a new file to selected project and right after I need to access the file.

    This is the code which adds the new file:

    public static void AddProjectItemFromTemplate(this Project project, string outputFileName, string content)
    {
    var directory = System.IO.Path.GetDirectoryName(project.FullName) + "\\";
    string path2 = System.IO.Path.Combine(directory, outputFileName);
    System.IO.File.WriteAllText(path2, content);
    // add output file to project
    project.ProjectItems.AddFromFile(path2);
    }
    

    This code successfully adds the file. But when I iterate through project items I cannot find the file which is just added. To see this file, execution process of the vs package must end.

    This is how I iterate through project items.

    foreach (ProjectItem projectItem in projectItems)
    {
        if (projectItem.Name.Equals(fullName))
        {
            return projectItem;
        }
    }
    

    Result of this function is null if I call it just after I added file. But file is actually added. I see it from solution explorer. I hope I managed to describe problem. Project itself is huge and I cannot share all of the code.

    What I tried so far is, added project.Save() right after call to addfrom file method. But no success.

    Thanks!

    Thursday, April 16, 2015 7:48 AM

Answers

  • projectItem.Name doesn't return a full name (with path), but a short name (without path). So your comparison fails. Could be that your problem? I have been always able to get the added project item just after adding it.

    * My new blog about VSX: http://www.visualstudioextensibility.com * Twitter: https://twitter.com/VSExtensibility * MZ-Tools productivity extension for Visual Studio: http://www.mztools.com.

    • Proposed as answer by Caillen Friday, April 17, 2015 1:42 AM
    • Marked as answer by Caillen Thursday, April 23, 2015 10:38 AM
    Thursday, April 16, 2015 12:47 PM
  • Hi hakdag,

    The problem should be with the "fullName" as Carlos said, I tested with this code snippet, and it worked fine to add a file and find the added file by iterating all the project items:

    DTE dte = GetService(typeof(SDTE)) as DTE;
                Project proj = dte.Solution.Projects.Item(1);
                //add a file named abc.cs
                proj.AddProjectItemFromTemplate("abc.cs", "Hello world");
    
    
                //iterate all the files, find abc.cs
                foreach (ProjectItem projectItem in proj.ProjectItems)
                {
                    if (projectItem.Name.Equals("abc.cs"))
                    {
                        Console.WriteLine("find abc.cs");
                    }
                }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Caillen Thursday, April 23, 2015 10:38 AM
    Friday, April 17, 2015 2:06 AM

All replies

  • projectItem.Name doesn't return a full name (with path), but a short name (without path). So your comparison fails. Could be that your problem? I have been always able to get the added project item just after adding it.

    * My new blog about VSX: http://www.visualstudioextensibility.com * Twitter: https://twitter.com/VSExtensibility * MZ-Tools productivity extension for Visual Studio: http://www.mztools.com.

    • Proposed as answer by Caillen Friday, April 17, 2015 1:42 AM
    • Marked as answer by Caillen Thursday, April 23, 2015 10:38 AM
    Thursday, April 16, 2015 12:47 PM
  • Hi hakdag,

    The problem should be with the "fullName" as Carlos said, I tested with this code snippet, and it worked fine to add a file and find the added file by iterating all the project items:

    DTE dte = GetService(typeof(SDTE)) as DTE;
                Project proj = dte.Solution.Projects.Item(1);
                //add a file named abc.cs
                proj.AddProjectItemFromTemplate("abc.cs", "Hello world");
    
    
                //iterate all the files, find abc.cs
                foreach (ProjectItem projectItem in proj.ProjectItems)
                {
                    if (projectItem.Name.Equals("abc.cs"))
                    {
                        Console.WriteLine("find abc.cs");
                    }
                }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Caillen Thursday, April 23, 2015 10:38 AM
    Friday, April 17, 2015 2:06 AM