Search code examples
c#visual-studio-extensionsvisual-studio-addins

Add file to project c#


In my Visual Studio add-in I would like to add an XML-File in the Properties folder of the current project at runtime.

With the following code the file appears after reloading the project ... so far so good

new XDocument(new XElement("Settings",
                     new XElement("Blabla",
                         new XElement("SomeSetting", value),
                 )
             ))
             .Save(Path.Combine(GetProjectPropertiesPath(), FileName));

var project = new Project(GetProjectPath());
project.ReevaluateIfNecessary();

if (project.Items.FirstOrDefault(i => i.EvaluatedInclude == Path.Combine(GetProjectPropertiesPath(), FileName)) == null)
{
    project.AddItem("Content", Path.Combine(GetProjectPropertiesPath(), FileName));
}

project.Save();

The Project-Class is in namespace Microsoft.Build.Evaluation.

Reload Project Message

XML-File appears

My problem: First i want that the added file is showing without reloading the project... i saw in several NuGet-Pakages or Extensions that this must be possible. Second, the added file is not recognized by the team foundation server and has to be added manually to the source control... The file should NOT be checked-in automatically.

Does anyone has an idea, how this can be solved? Thank you very much!


Solution

  • @Leo Liu-MSFT ... Thank you for suggested solution, but unfortunately that was not the way I wanted to solve it.

    For those who are interested, I have solved it now like this:

        private static void AddXmlFileToProject(string tempPath)
        {
            try
            {
                var project = GetProject();
                foreach (EnvDTE.ProjectItem projectItem in project.ProjectItems)
                {
                    if (projectItem.Name == "Properties")
                    {
                        foreach (EnvDTE.ProjectItem item in projectItem.ProjectItems)
                        {
                            if (item.Name == FileName)
                            {
                                EditExistingFile(item);
                                return;
                            }
                        }
    
                        projectItem.ProjectItems.AddFromFileCopy(tempPath);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
        private static void EditExistingFile(EnvDTE.ProjectItem projectItem)
        {
            try
            {
                projectItem.Open();
    
                var textSelection = (TextSelection)projectItem.Document.Selection;
                projectItem.Document.ReadOnly = false;
    
                textSelection.SelectAll();
                textSelection.Text = //new XML Content here
    
                projectItem.Document.Save();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    

    So the file is added to the project without reloading and is recognized by source control, as you can see here:

    Solution video

    I hope it helps someone else