using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; using Microsoft.Win32.SafeHandles; using System.Runtime.InteropServices; [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); public const short FILE_ATTRIBUTE_NORMAL = 0x80; public const short INVALID_HANDLE_VALUE = -1; public const uint GENERIC_READ = 0x80000000; public const uint GENERIC_WRITE = 0x40000000; public const uint CREATE_NEW = 1; public const uint CREATE_ALWAYS = 2; public const uint OPEN_EXISTING = 3; public const uint OPEN_OpenOrCreate = 4; private void WriteData(string text) { IntPtr ptr = CreateFile(path, GENERIC_WRITE, 1, IntPtr.Zero, OPEN_OpenOrCreate, 0, IntPtr.Zero); SafeFileHandle safehandle = new SafeFileHandle(ptr, true); if (!safehandle.IsInvalid) { safehandle.Close(); FileStream fs1 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); byte[] bytes = Encoding.UTF8.GetBytes(text); fs1.Seek(0, SeekOrigin.End); fs1.Write(bytes, 0, bytes.Length); fs1.Flush(); fs1.Close(); } else { Thread.Sleep(1); WriteData(text); } }