How to retrieve variables from the Watch List in Visual Studio using DTE?

MypkaXD 0 Reputation points
2024-10-03T12:01:36.7133333+00:00

Hi everyone!

I'm working with DTE in Visual Studio and trying to access variables from the Watch List window. I have code to activate the Watch window, but I'm unsure how to extract the variables and their values.

Here's a snippet of my code:

var watchWindow = m_DTE_dte.Windows.Item("{90243340-BD7A-11D0-93EF-00A0C90F2734}");
watchWindow.Visible = true;
// ... (more code)
//

How can I iterate through the variables in the Watch List and retrieve their names and values? I would appreciate any hints or code examples!

Thank you!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,913 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 31,246 Reputation points Microsoft Vendor
    2024-10-04T02:39:00.43+00:00

    Hi @MypkaXD ,

    To iterate through the variables in the Watch List and retrieve their names and values using DTE (Development Tools Environment) in Visual Studio, you can access the Debug object. The Watch window contains expressions, and you can retrieve these through the Expression objects available via Debugger3.

    EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)m_DTE_dte;
    EnvDTE80.Debugger3 debugger = (EnvDTE80.Debugger3)dte2.Debugger;
    EnvDTE.Expression watchExpression;
    
    var watchWindow = dte2.Windows.Item("{90243340-BD7A-11D0-93EF-00A0C90F2734}");
    watchWindow.Visible = true;
    
    var watchExpressions = debugger.Watchers;
    
    foreach (EnvDTE.Expression exp in watchExpressions)
    {
        // Access the name and value of each expression
        string variableName = exp.Name;
        string variableValue = exp.Value;
    
        Console.WriteLine($"Variable Name: {variableName}, Value: {variableValue}");
    }
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.