// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace E { public class Program { struct IO_COUNTERS { public ulong ReadOperationCount; public ulong WriteOperationCount; public ulong OtherOperationCount; public ulong ReadTransferCount; public ulong WriteTransferCount; public ulong OtherTransferCount; } [DllImport("kernel32.dll")] private static extern bool GetProcessIoCounters(IntPtr ProcessHandle, out IO_COUNTERS IoCounters); private const int NLIN = 10000000; private static void Test(StreamWriter sw) { for(int i = 0; i < NLIN; i++) { sw.WriteLine("This is a test"); } } private static ulong GetWrites() { IO_COUNTERS counters; GetProcessIoCounters(Process.GetCurrentProcess().Handle, out counters); return counters.WriteOperationCount; } public static void Main(string[] args) { ulong io1 = GetWrites(); using(StreamWriter sw = new StreamWriter(@"C:\work\unbuf.dat", false, Encoding.UTF8)) { Test(sw); } ulong io2 = GetWrites(); Console.WriteLine(io2-io1); ulong io3 = GetWrites(); using(StreamWriter sw = new StreamWriter(@"C:\work\buf.dat", false, Encoding.UTF8, 65536)) { Test(sw); } ulong io4 = GetWrites(); Console.WriteLine(io4-io3); } } }