tags:

views:

314

answers:

1

I'm trying to write a quick cgi app in c#. I need to get to the stdout stream and write some binary data. The only thing I can find to do this is Console.Write, which takes text. I've also tried

Process.GetCurrentProcess().StandardOutput.BaseStream.Write

which doesn't work either. Is this even possible?

+1  A: 

Something like the following (very quick example written in notepad):

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class MyObject {
    public int n1 = 0;
    public int n2 = 0;
    public String str = null;
}

public class Example
{
    public static void Main()
    {
        MyObject obj = new MyObject();
        obj.n1 = 1;
        obj.n2 = 24;
        obj.str = "Some String";
        BinaryFormatter formatter = new BinaryFormatter();

        StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
        sw.AutoFlush = true;
        Console.SetOut(sw);

        formatter.Serialize(sw.BaseStream, obj);
    }
}
Peter McGrattan
FYI - You might be interested in LINQPad. It's a great little tool for quickly writing and testing code snippets.
Winston Smith