MikeD
MikeD

Reputation: 973

Is there a way to dump a stream from the debugger in VS

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.

Upvotes: 11

Views: 11371

Answers (4)

argaz
argaz

Reputation: 1488

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)

Upvotes: 0

James
James

Reputation: 263

Type this into the Immediate Window:

System.Diagnostics.Debug.WriteLine((new System.IO.StreamReader(stream)).ReadToEnd());

Upvotes: 20

David Hay
David Hay

Reputation: 3047

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.

Upvotes: 0

David Silva Smith
David Silva Smith

Reputation: 11716

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

Upvotes: 4

Related Questions