Results 1 to 12 of 12

Thread: FileStream/StreamWriter Problem

  1. #1

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

    Unhappy FileStream/StreamWriter Problem

    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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Why are you using the API to create the file instead of using the FileStream to create it?
    VB Code:
    1. Dim fs As New FileStream("C:\xyz.txt", FileMode.Create)
    2.         Dim sw As New StreamWriter(fs)
    3.         sw.WriteLine("Simple Text")
    4.         sw.Close()
    5.         'closing the streamwriter will close the file but
    6.         'you can still close on the filestream if you want
    7.         fs.Close()

    There are also these FileModes:
    OpenOrCreate and CreateNew
    Last edited by Edneeis; Jul 13th, 2003 at 12:56 AM.

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you could do it like this also , just shortned down slightly :
    VB Code:
    1. Dim sWrite As IO.StreamWriter = New IO.StreamWriter(New IO.FileStream("C:\im_a_text.txt", IO.FileMode.OpenOrCreate))
    2.             sWrite.Write("some text")
    3.             sWrite.Close()

    or if you just wish to create a file of any type , eg: txt you can also do this :
    VB Code:
    1. Dim f As IO.File
    2.             f.Create("C:\im_a_text.txt")'/// create a new text file.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168
    Thanks for your response.

    Basicaly I want to send this "Simple Text" to devices like (LPT1: or
    COM1 This type of printing I want to use with Dot Matrix Printer. It Bypass Windows Printe Manager and print fasts simple text.
    (The concept I am using in VB6 to print my clients Invoice)

    When I use "LPT1:" or "COM1:" to as a parameter instead of file name VB.Net gives me following message when using it like this

    Dim fs As New FileStream("LPT1:", FileMode.Open)


    Error Message:
    An unhandled exception of type
    'System.NotSupportedException occured in mscorlib.dll

    Additional information: FileStream was asked to open a device
    that take a string will only work with devices that are really files.
    If you need support for devices like "com1:" or "lpt1:" then
    call CreateFile yourself then use the FileStream constructors that
    take an OS handle as an IntPtr.
    The message shows It is possible.

    Thanks again and hope for the solution.

  5. #5

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168
    One updation please

    I found the reason why it is not writing.

    Code:
    FS = New FileStream(fh, FileAccess.Write)
    In above code fh has a file handle, say it is 3776. Now the code is accepting it as file name not as file handle. It writes "Some Text" in 3776 file in project's bin directory

    Now the QUESTTION is why not the above code handling fh as file handle instead of string ?

    Please tell

    Thanks

  6. #6

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try using an IntPtr type instead of Integer.

  8. #8

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

    Smile FileStream/StreamWriter Problem [Resolved]

    Thanks Edneeis


    It is working fine and with also "LPT1:"

    Thanks again.

  9. #9

  10. #10
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    VB Code:
    1. Dim fh As Integer
    2.         Dim SW As StreamWriter
    3.         Dim FS As FileStream
    4.         fh = Win32API.CreateFile("C:\xyz.txt", Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0)
    5.         FS = New FileStream(fh, FileAccess.Write)
    6.         FS.Flush()
    7.         SW = New StreamWriter(FS)
    8.         SW.WriteLine("Simple Text")
    9.         FS.Flush()
    10.         SW.Close()
    11.         FS.Close()
    12.         Win32API.CloseHandle(fh)

    With this code posted by Parminder, I get the same problem - prints "Simple Text" to a file with the name of the file handle in the app's bin directory AND creates a blank file "C:\xyz.txt".

    If I pass "LPT1:" as the first arg to CreateFile, I just get the file in the bin directory but no output to the printer at LPT1.

    It won't allow IntPtr as the return type of CreateFile even with Option Strict Off.

  11. #11
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    I managed to resolve this as follows:

    Call WriteToPort("LPT1:", "The text that i want to output to LPT1 port")

    The

    VB Code:
    1. Public Class Win32API
    2.  
    3.         Public Const GENERIC_WRITE = &H40000000
    4.         Public Const CREATE_ALWAYS = 2
    5.         Public Const OPEN_EXISTING = 3
    6.  
    7.         Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
    8.         (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, _
    9.         ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, _
    10.         ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, _
    11.         ByVal hTemplateFile As Integer) As Integer
    12.  
    13.         Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" _
    14.         (ByVal hObject As Long) As Long
    15.  
    16.     End Class
    17.  
    18.     Public Sub WriteToPort(ByRef sPort As String, ByRef sText As String)
    19.  
    20.         Dim FileHandle As Long
    21.         Dim FileHandleIntPtr As IntPtr
    22.         Dim FStream As FileStream
    23.         Dim SWriter As StreamWriter
    24.  
    25.         FileHandle = Win32API.CreateFile(sPort, Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0)
    26.  
    27.         FileHandleIntPtr = New IntPtr(FileHandle)
    28.  
    29.         FStream = New FileStream(FileHandleIntPtr, FileAccess.Write, False)
    30.  
    31.         FStream.Flush()
    32.  
    33.         SWriter = New StreamWriter(FStream)
    34.  
    35.         SWriter.WriteLine(sText)
    36.  
    37.         FStream.Flush()
    38.  
    39.         SWriter.Close()
    40.  
    41.         FStream.Close()
    42.  
    43.         Win32API.CloseHandle(FileHandle)
    44.  
    45.     End Sub

  12. #12

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168
    dear robertx

    use api declaration like this (only)

    Code:
        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 IntPtr

    all the best

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width