locked
For VS Extensions Proper Way to Store Data in Project File RRS feed

  • Question

  • What is the proper way to store persistent data in a project file?

    I know that there is the EnvDTE.Project.Globals object, where an extension can set an attribute and have that save to the project file. Yes, one has to say that the data is persistent and then save the project file. The problem with this approach is that the the Globals object does not exist for F# projects and for all intent and purpose does not exist for C++ files (it is not null, but persistence does not work). Globals works nicely for C# and VB. Moreover, I do not like the way that Globals writes to the project file.

    EnvDTE.Project oProject = dte.Solution.Projects.Item(1);
    Globals oGlobals = oProject.Globals;
    oGlobals["myvariableA"] = "asdf";
    oGlobals.VariablePersists[propertyName] = true;

    The final output would be:

      <ProjectExtensions>
        <VisualStudio>
          <UserProperties myvariableA="assdf" />
        </VisualStudio>
      </ProjectExtensions>

    All extensions use the same block. My idea would be to place the data in some format like the following, which also does not cause  validation issues, if a user has the project open in the Visual Studio text editor.

    <ProjectExtensions>
        <VisualStudio>
            <MyProject>
                <MyProjectProperty1>3</MyProjectProperty>
                <MyProjectProperty2>8</MyProjectProperty>
                <MyProjectProperty3>2016</MyProjectProperty>
            </MyProject>
        </VisualStudio>
    </ProjectExtensions>

    I thought of using IPersistXMLFragment and IVsProjectFlavorCfg combinations, but this approach lacks the key concept of reading, writing, and removing the XML code from the project file. The IPersistXMLFragment load method takes XML code as a string, loads the values into a dictionary, and then manages the values. The save method does the reverse going from a management construct back to a string based version of XML. Missing, as I just said, is the read, write, and remove from the actual project file without manually editing the file and causing a project reload.

    I was also playing with the IVsHierarchy and MSBuild VSIX interface/object, but did not get very far.

    Does anyone have any ideas?



    Friday, April 8, 2016 10:28 PM

All replies

  • Hi Sarah Weinberger,

    All the changes of project file will cause project reload. I suggest you add a custom xml file in project to store data. For example, proj.xml. You could define the xml schema by yourself.

    Best Regards,
    Li Wang


    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.

    Monday, April 11, 2016 7:43 AM
  • What is the proper way to store persistent data in a project file?

    I know that there is the EnvDTE.Project.Globals object, where an extension can set an attribute and have that save to the project file. Yes, one has to say that the data is persistent and then save the project file. The problem with this approach is that the the Globals object does not exist for F# projects and for all intent and purpose does not exist for C++ files (it is not null, but persistence does not work). Globals works nicely for C# and VB. Moreover, I do not like the way that Globals writes to the project file.

    EnvDTE.Project oProject = dte.Solution.Projects.Item(1);
    Globals oGlobals = oProject.Globals;
    oGlobals["myvariableA"] = "asdf";
    oGlobals.VariablePersists[propertyName] = true;

    The final output would be:

      <ProjectExtensions>
        <VisualStudio>
          <UserProperties myvariableA="assdf" />
        </VisualStudio>
      </ProjectExtensions>

    All extensions use the same block. My idea would be to place the data in some format like the following, which also does not cause  validation issues, if a user has the project open in the Visual Studio text editor.

    <ProjectExtensions>
        <VisualStudio>
            <MyProject>
                <MyProjectProperty1>3</MyProjectProperty>
                <MyProjectProperty2>8</MyProjectProperty>
                <MyProjectProperty3>2016</MyProjectProperty>
            </MyProject>
        </VisualStudio>
    </ProjectExtensions>

    I thought of using IPersistXMLFragment and IVsProjectFlavorCfg combinations, but this approach lacks the key concept of reading, writing, and removing the XML code from the project file. The IPersistXMLFragment load method takes XML code as a string, loads the values into a dictionary, and then manages the values. The save method does the reverse going from a management construct back to a string based version of XML. Missing, as I just said, is the read, write, and remove from the actual project file without manually editing the file and causing a project reload.

    I was also playing with the IVsHierarchy and MSBuild VSIX interface/object, but did not get very far.

    Does anyone have any ideas?



          oGlobals.VariablePersists[propertyName] = true; // what is this code mean?

    前无古人,后无来者

    Tuesday, March 13, 2018 9:07 AM