Output a watched Visual Studio variable to a file

前端 未结 4 1890
迷失自我
迷失自我 2020-12-09 17:50

Is there a way in Visual Studio (2008 if it matters) that I can, in debug/break mode, write the contents of a variable to a text/XML file?

The scenario is that I ha

相关标签:
4条回答
  • 2020-12-09 18:11

    Thanks to Richard's answer, this is working for me.

    System.IO.File.WriteAllBytes(@"c:\Temp\temp.txt", myVar);

    Make sure that C:\Temp exists.

    The reason for writing to a folder and not to the root C:\ is to avoid UnauthorizedAccessException when not running Visual Studio as administrator.

    0 讨论(0)
  • 2020-12-09 18:12

    One way to do it would be to use the immediate window (menu Debug -> Windows -> Immediate). In the window that appears you can use the "?" to query the value of a variable.

    Assuming your history variable is a string you view its contents by typing the following in the immediate window:

    ?history
    

    You could copy and paste the output from there into a text file or alternatively ask Visual Studio to log all command window output. To do this type:

    >log "c:\test.log"
    >? history
    >log off
    

    Log is an alias for Tools.LogCommandWindowOutput and accepts the following parameters:

    Tools.LogCommandWindowOutput [filename] [/on|/off] [/overwrite]
    

    Check out the MSDN article Log Command Window Output Command for more information.  

    0 讨论(0)
  • 2020-12-09 18:28

    I think that my answer is pretty much the same as JamesPickrell's, but from the Immediate Window you could also do something like this:

    My.Computer.FileSystem.WriteAllText("c:\temp.txt",history,True)
    

    This would output the content of the "history" variable to a file called c:\temp.txt.

    0 讨论(0)
  • 2020-12-09 18:30

    I found useful/demonstrative/shareable to save the variable as a json string to the file. From Immediate Window enter the following:

    string jsonedVariable = Newtonsoft.Json.JsonConvert.SerializeObject(VARIABLE);
    System.IO.File.WriteAllText(@"C:\FILENAME.json", jsonedVariable);
    
    0 讨论(0)
提交回复
热议问题