简体   繁体   中英

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!

Add file to project c#

Yes, you can use nuget package to add this XML-File in the Properties folder of the current project at run-time.

See Creating NuGet packages for some more details.

Following is my test .nuspec :

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyTestPackager</id>
    <version>1.0.0</version>
    <authors>Tester</authors>
    <owners>Tester</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
  <files>
    <file src="Test.xml" target="content\Properties\Test.xml" />
  </files>
</package>

Then create the nuget package, and add this package to the project, the XML-File will be add to the Properties folder:

在此处输入图片说明

With this way, the XML-file add to the project, you can check it from Solution explorer.

Besides, this XML file will be recognized by the team foundation server, we could add it to the the source control.

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM