Hi All
I have a problem using FileStream and StreamWriter object.
I am creating file handle with CreateFile (Win32API) function and
passing to FileStream Constructor as an IntPtr
but the code creates only file xyz.txt , not write anything into it.
Please tell me what is wrong in code
Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fh As Integer Dim SW As StreamWriter Dim FS As FileStream fh = Win32API.CreateFile("C:\xyz.txt", Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0) FS = New FileStream(fh, FileAccess.Write) FS.Flush() SW = New StreamWriter(FS) SW.WriteLine("Simple Text") FS.Flush() SW.Close() FS.Close() Win32API.CloseHandle(fh) End Sub
Win32API Class Code
Code:Public Class Win32API Public Const GENERIC_WRITE = &H40000000 Public Const CREATE_ALWAYS = 2 Public Const OPEN_EXISTING = 3 Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long End Class




Reply With Quote