locked
File modified dialog when Addin Regenerates open file in project RRS feed

  • Question

  • Hi,

    I have implemented a Visual Studio 2005 Addin, which generates a .cs file when I save a custom defined file in the project.

    If this generated file is not part of the solution, the Addin adds the file using ProjectItems.AddFromFile(Filename).
    If it is already part of the solution (has been generated before), then I just over write the file usinf File.Open()

    The problem is that if this generated file was open in Visual Studio, a dialog box comes up saying that
    the "file has been modified outside the source editor".


    I want to suppress this dialog box. Is there any way this can be done?

    Please let me know if you need more inputs.

    Any help would be of great help :)

    Thanks!
    Wednesday, February 10, 2010 11:22 AM

Answers

  • The easiest thing is to probably just close the file if it's open, do the generation, and then optionally re-open it.

    Assuming you're using an Add-In (this is from VS2008 but it should be more or less the same):

                        string filename = @"... your filename ... ";
    
                        EnvDTE.Document theDocument = null;
    
                        try
                        {
                            theDocument = _applicationObject.Documents.Item(filename);
                        }
                        catch (System.ArgumentException)
                        {
                            // The file you're looking for does not exist. It's not open. 
    
                            // This will dump out the open documents. 
                            // foreach (EnvDTE.Document doc in _applicationObject.Documents)
                            // {
                            //    string name = doc.FullName;
                            // }
                            return;
                        }
    
                        string theKind = theDocument.Kind;
    
                        theDocument.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                        
                        //
                        // Do your generation 
                        //
    
                        // Reopen it if you wish. 
                        EnvDTE.Window theWindow = _applicationObject.OpenFile(EnvDTE.Constants.vsViewKindCode, filename);
                        theWindow.Visible = true;
    
    • Proposed as answer by Grey Ham Wednesday, February 10, 2010 1:15 PM
    • Marked as answer by Nancy Shao Friday, February 19, 2010 2:30 AM
    Wednesday, February 10, 2010 1:01 PM