[pypy-dev] Example of .NET filehandles

Ben.Young at risk.sungard.com Ben.Young at risk.sungard.com
Mon Jul 24 14:48:57 CEST 2006


Hi Antonio,

Here's the example I mentioned. I don't think it's particularly safe, so 
it may be better to keep a map of ints to filestreams that you have 
opened, and use that if possible, and fall back to this if you can't.

Cheers,
Ben

using System;
using System.Collections.Generic;
using System.Text;

namespace posix
{
    class Program
    {
        static void Main(string[] args)
        {
            System.IO.FileStream stream1 = new System.IO.FileStream
(args[0], System.IO.FileMode.Create);
            int handle = 
stream1.SafeFileHandle.DangerousGetHandle().ToInt32();

            System.IO.FileStream stream2 = new System.IO.FileStream(new 
Microsoft.Win32.SafeHandles.SafeFileHandle(new IntPtr(handle), true), 
System.IO.FileAccess.Write);
            stream2.Write(new byte[]{(byte)'H', (byte)'e', (byte)'l', (
byte)'l', (byte)'o'}, 0, 5);
            stream2.Close();
        }
    }
}



More information about the Pypy-dev mailing list