UndoContext.IsOpen Property

Gets whether an undo operation is currently in effect or if a solution is open.

Namespace:  EnvDTE
Assembly:  EnvDTE (in EnvDTE.dll)

bool IsOpen { get; }

Property Value

Type: System.Boolean
A Boolean value indicating true if an undo operation is in effect or if a solution is open, false if not.

An undo operation is in effect if the UndoContext object is open.

An open solution does not necessarily have a solution (.sln) file, because it might not have been saved yet.

public void IsOpenExample(DTE2 dte)
{
    // Create a new text file.
    dte.ItemOperations.NewFile(@"General\Text File", "", 
        Constants.vsViewKindPrimary);

    TextDocument doc = 
        (TextDocument)dte.ActiveDocument.Object("TextDocument");
    bool done = false;

    while (!done)
    {
        if (!dte.UndoContext.IsOpen)
        {
            try
            {
                dte.UndoContext.Open("Insert 10 Lines", false);

                // Insert 10 lines of text into the new document.
                EditPoint point = doc.StartPoint.CreateEditPoint();

                for (int i = 0; i < 10; ++i)
                    point.Insert("This is a test.\n");

                done = true;
            }
            catch
            {
            }
            finally
            {
                if (done) dte.UndoContext.Close();
            }
        }
    }
}

Community Additions

ADD
Show: