11

I'm using VS 2010 and am working with a lot of streams in C# in my current project. I've written some stream dump utilities for writing out certain types of streams for debugging purposes, but I seem to keep stumbling across times when I am debugging and need to look into the stream I am debugging over, but I didn't put my dump calls in there. It seems like I should be able to dump the stream somehow just using VS or maybe tell it to call one of my dump methods on a stream in the debugger. Is there a wy to do this?

The streams I am working with have some text describing a blob of data and then the bytes of the blob, so looking at the description is useful. My dump methods typically just dump that information out and then skip the blobs.

MikeD
  • 923
  • 4
  • 12
  • 24

4 Answers4

20

Type this into the Immediate Window:

System.Diagnostics.Debug.WriteLine((new System.IO.StreamReader(stream)).ReadToEnd());
skolima
  • 31,963
  • 27
  • 115
  • 151
James
  • 263
  • 3
  • 6
  • 1
    You may need `System.Diagnostics.Debug.WriteLine((new System.IO.StreamReader(stream)).ReadToEnd());` – KCD Oct 28 '12 at 21:47
  • 4
    Note that this will consume the call so you should know that you likely won't be able to continue the current debugging session. – Jeroen Vannevel Jun 19 '14 at 15:53
4

Maybe you could write a Visualizer? MSDN explains how here: http://msdn.microsoft.com/en-us/library/e2zc529c.aspx

David Silva Smith
  • 11,498
  • 11
  • 67
  • 91
  • Very nice, this is a great solution that allows me to make my dump function be a part of the debugger – MikeD Nov 25 '09 at 23:01
0

You could just use the immediate window to call your dump function while debugging:

MikeDsDumpFxn(whateverStreamIsActiveInThisContext)

If your function returns a string it will print right there as the result in the immediate window.

David Hay
  • 3,027
  • 2
  • 27
  • 29
0

If you have binary data in the stream you can try dumping it into a file using the following lines in the immediate window:

var lastPos = stream.Position;
stream.Seek(0, SeekOrigin.Begin)
File.WriteAllBytes("filepath.bin", new BinaryReader(stream).ReadBytes((int)stream.Length))
stream.Seek(lastPos, SeekOrigin.Begin)

The stream obviously has to be seekable to prevent the side effects of changing the position of the stream when dumping it (reverted in the last line).

If the stream doesn't have the Length property you can use a similar solution to the one done here:

var lastPos = stream.Position;    
var ms = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin)
stream.CopyTo(ms)
File.WriteAllBytes("filepath.bin", ms.ToArray())
stream.Seek(lastPos, SeekOrigin.Begin)
argaz
  • 1,458
  • 10
  • 15