3

From a VS package code, I wonder if there is an API to write the currently opened VS solution file without having the File Modification Detected dialog.

VS File Modification Detected

I need to write a custom GlobalSection:

GlobalSection(MyProduct) = preSolution
    MyData = "XYZ"
EndGlobalSection

I am aware of IVsDocDataFileChangeControl and the associated sample source code, but it seems the solution file object cannot be cast to IVsPersistDocData. enter image description here

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • Maybe you can try http://msdn.microsoft.com/en-us/library/envdte._solution.saveas.aspx ? (Also has Saved and IsDirty properties) – ErikEJ Sep 29 '14 at 09:47

1 Answers1

5

You are looking for the IVsSolutionPersistence service, which you can obtain by passing typeof(SVsSolutionPersistence) to GetService. I'll try look into the details more later today.

Facilitates writing package-specific properties and options into the solution file (.sln), and, conversely, the loading of properties and options from the file.


Ok, I finally found the time to dig into IVsSolutionPersistence. The amount of effort to use this interface is ridiculously high, so hopefully this answer will help some others people not wasting hours on this.

Basically you can save your options in the .sln Solution file, or in the .suo Solution User Option file. The .sln Solution file is suited to share options with everyone using the solution, the .suo Solution User Option file is suited to store/restore options only on the user local machine.

STORAGE IN .SUO FILE

To implement storage through the .suo Solution User Option file, you need that your Package class implements the interface IVsPersistSolutionOpts. The Microsoft.VisualStudio.Shell.Package class already implements this interface but you need to override all four methods in your package class. To get a sample implementation of this, you can refer to source code of this Code Project article. Remarks:

  • The stream provided by VS needs to be wrapped by a StreamEater class defined in this project? Apparently this ComStreamWrapper class does the same.
  • When saving in .suo file, the user doesn't get any prompt dialog to ask to save the changes.
  • When debugging your package through the VS experimental, you'll see that the .suo file is not getting actually written, I think VS experimental doesn't want to touch it (just my guess, not sure).
  • .suo file storage is a proprietary binary storage. You can still open such file through a text reader and hopefully see your key/value data somehow.

STORAGE IN .SLN FILE

To implement storage through the .sln Solution file, you need that your Package class implements the interface IVsPersistSolutionProps. This interface implements the IVsPersistSolutionOpts interface but it is not implement by the class Microsoft.VisualStudio.Shell.Package. The best existing implementation of this I found is in the project VisualGit source code. Remarks:

  • You'll need to tag your package class with one or several ProvideSolutionPropertiesAttribute["YourDataKey"]. If you don't do that your implementation of the method ReadSolutionProps() won't be called.
  • You'll need to wrap read/write accesses to Microsoft.VisualStudio.OLE.Interop.IPropertyBag through the class PropertyBag. You'll have the choice to save data with quote, or just rawData. The section added in the .sln file will look like:

      GlobalSection(MY_SECTION_NAME) = preSolution
         MY_DATA = "12:02:45"
      EndGlobalSection
    
  • In your implementation of the method ReadSolutionProps() of the method, you'll have to pass a VSQUERYSAVESLNPROPS value that will indicate to VS the storage state.

  • The .sln file will be actually written when the user will close the solution, or close VS with the solution opened. The user will be prompted if she wants to modify the .sln file. If the user do Save-All, she won't be prompted for .sln file modifications, neither at Save-All time, neither at Close Solution time.
Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Do you know how IVsSolutionPersistence behaves with the File Modification Detected dialog and the source control checkout? If no, I'll check this later and will post my findings here. Thanks – Patrick from NDepend team Sep 30 '14 at 08:08