-2

I use ESC/P or Epson Standard Code for Printers which aims to make bold. But I found there was an error "the given path's format is not supported". Is there a best solution?

Thanks

the given path's format is not supported

Dim ESC As String = "\u001B"
Dim BoldOn As String = (ESC + ("E" + "\u0001"))
Dim BoldOff As String = (ESC + ("E" + "\0"))
 Public Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal szFileName As String) As Boolean
        ' Open the file.
        Using 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 Using
    End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim printer As String = "Generic / Text Only"
        For i As Integer = 1 To 1
            SendFileToPrinter(printer, (BoldOn + ("C:\vDos\#LPT1.asc" + BoldOff)))
'SendFileToPrinter(printer, "C:\vDos\#LPT1.asc") if I use this code then the error does not appear 
        Next i
  
    End Sub
roy
  • 693
  • 2
  • 11
  • If you want the contents of a file as a `Byte` array, call `File.ReadAllBytes`. May well not solve the issue you asked about but it will cut out a lot of unnecessary code. – John Jul 27 '22 at 06:49
  • @John , `SendFileToPrinter(printer, "C:\vDos\#LPT1.asc")` if I use code like this then no error this occurs error because I use ESC/P code formatting – roy Jul 27 '22 at 06:59
  • Change this SendFileToPrinter(printer, (BoldOn + ("C:\vDos\#LPT1.asc" + BoldOff))) to this SendFileToPrinter(printer, "C:\vDos\LPT1.asc"), assuming that LPT1.asc exists and is actually located in C:\vDOS folder. – F0r3v3r-A-N00b Jul 27 '22 at 07:27
  • @F0r3v3r-A-N00b ,`is actually located in C:\vDOS folder.` . yes that's right it's the actual location of the file "#LPT1. ASC" – roy Jul 27 '22 at 07:33
  • Can't you change the name of your file to another? Do not put # in your filename and do not name it LPT1. – F0r3v3r-A-N00b Jul 28 '22 at 02:31

1 Answers1

0

Your are passing the result of (BoldOn + ("C:\vDos\#LPT1.asc" + BoldOff)) to the szFileName parameter of your method but the one and only place that parameter is being used is here:

Using fs As New FileStream(szFileName, FileMode.Open)

The prefix and suffix you're adding are creating a value that is not a valid file path as far as the FileStream constructor is concerned, hence the exception. You need to pass a valid file path to that constructor.

It appears that that prefix and suffix are supposed to be passed to the printer somehow, but you're not doing that. My guess would be that you need to add that prefix and suffix to the actual data from the file, not the file path, e.g.

Dim BoldOn As Byte() = Encoding.UTF8.GetBytes(ESC + ("E" + "\u0001"))
Dim BoldOff As Byte() = Encoding.UTF8.GetBytes(ESC + ("E" + "\0"))

Dim fileContents = File.ReadAllBytes(filePath)
Dim dataToPrint = BoldOn.Concat(fileContents).Concat(BoldOff).ToArray()

It appears that I need spell it out in detail, so here's your original code modified to incorporate what I explained above:

Private Shared ESC As String = "\u001B"
Private Shared BoldOn As Byte() = Encoding.UTF8.GetBytes(ESC + ("E" + "\u0001"))
Private Shared BoldOff As Byte() = Encoding.UTF8.GetBytes(ESC + ("E" + "\0"))

Public Shared Function SendFileToPrinter(printerName As String, filePath As String) As Boolean
    Dim bytes = BoldOn.Concat(File.ReadAllBytes(filePath)).Concat(BoldOff).ToArray()
    Dim byteCount = bytes.Length
    Dim unmanagedBytesPointer = Marshal.AllocCoTaskMem(byteCount)

    Marshal.Copy(bytes, 0, unmanagedBytesPointer, byteCount)

    Dim success = SendBytesToPrinter(printerName, unmanagedBytesPointer, byteCount)

    Marshal.FreeCoTaskMem(unmanagedBytesPointer)

    Return success
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim printer = "Generic / Text Only"

    SendFileToPrinter(printer, "C:\vDos\#LPT1.asc")
End Sub

Note that it is just an educated guess that you need to combine those printer commands with the file contents. It's up to you to confirm that and, if it's not the case, find out what is required.

John
  • 3,057
  • 1
  • 4
  • 10
  • thanks for your answer but there was an error. `Value of type '1-dimensional array of Byte' cannot be converted to 'String'.` in the code line `SendFileToPrinter(printer, dataToPrint)` – roy Jul 27 '22 at 07:21
  • @user19541848, let's put our thinking caps on, shall we? You've already got a method `SendBytesToPrinter` that accepts a `Byte` array, so why would you call `SendFileToPrinter` and pass a `Byte` array? The whole point of that `SendFileToPrinter` method is that you pass it a file path and then it reads the data from that file. Maybe do that. – John Jul 27 '22 at 08:22
  • `Argument not specified for parameter 'dwCount' of 'Public Shared Function SendBytesToPrinter(szPrinterName As String, pBytes As System.IntPtr, dwCount As Integer) As Boolean'.` and `Value of type '1-dimensional array of Byte' cannot be converted to 'System.IntPtr'.` I have 2 errors . I use code like this `SendBytesToPrinter(printer, dataToPrint)` – roy Jul 27 '22 at 12:03
  • @user19541848, see my edit. – John Jul 28 '22 at 04:07
  • thanks for your reply, the important thing is that your code can read the contents of the file. Most I am looking for a solution to run the printer command – roy Jul 28 '22 at 07:59