how to auto detect printer data update and without event button in vb.net

81 views Asked by At

how to auto detect printer data update and without event button. and if I use the event button and I continue to process it once again then the process cannot be done because it is used by other processes such as the screenshot I attached below. when the printer data is updated, it automatically prints. There may be the best solution.

thanks

warning cannot access

path printer data : "C:\vDos"
file name printer data : #LPT1.asc
 Public Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal szFileName As String) As Boolean
        ' Open the file.
        Dim fs As New FileStream(szFileName, FileMode.Open)
        ' Create a BinaryReader on the file.
        Dim br As New BinaryReader(fs)
        ' Dim an array of bytes big enough to hold the file's contents.
        Dim bytes(fs.Length - 1) As Byte
        Dim bSuccess As Boolean = False
        ' Your unmanaged pointer.
        Dim pUnmanagedBytes As New IntPtr(0)
        Dim nLength As Integer

        nLength = Convert.ToInt32(fs.Length)
        ' Read the contents of the file into the array.
        bytes = br.ReadBytes(nLength)
        ' Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength)
        ' Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength)
        ' Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength)
        ' Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes)
        Return bSuccess
    End Function

    Public Shared Function SendStringToPrinter(ByVal szPrinterName As String, ByVal szString As String) As Boolean
        Dim pBytes As IntPtr
        Dim dwCount As Int32

        ' How many characters are in the string?
        ' dwCount = szString.Length;
        dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize

        ' Assume that the printer is expecting ANSI text, and then convert
        ' the string to ANSI text.
        pBytes = Marshal.StringToCoTaskMemAnsi(szString)
        ' Send the converted ANSI string to the printer.
        SendBytesToPrinter(szPrinterName, pBytes, dwCount)
        Marshal.FreeCoTaskMem(pBytes)
        Return True
    End Function
 Private Sub buttonSEND_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSEND.Click
        'Dim open As New System.Windows.Forms.OpenFileDialog()
        'Dim szFileName As String = "C:\vDos\#LPT1.asc"
        'If open.ShowDialog().Equals(DialogResult.OK) Then
        '    Dim sr As New StreamReader(open.FileName)
        '    szFileName = sr.ReadToEnd()
        '    sr.Close()
        'End If
        Dim printer As String = "Generic / Text Only"

        For i As Integer = 1 To 1
            SendFileToPrinter(printer, "C:\vDos\#LPT1.asc")
        Next i

    End Sub
0

There are 0 answers