Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

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

$
0
0

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!


Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>