vb.net 不支持给定路径的格式

the given path's format is not supported in vb.net

提问人:roy 提问时间:7/27/2022 最后编辑:roy 更新时间:7/28/2022 访问量:259

问:

我使用 ESC/P 或 Epson Standard Code for Printers,旨在加粗。但是我发现有一个错误“不支持给定路径的格式”。有没有最好的解决方案?

谢谢

不支持给定路径的格式

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
vb.net visual-studio-2010 winapi 打印后台处理程序 api

评论

0赞 John 7/27/2022
如果要将文件的内容作为数组,请调用 。可能无法解决您提出的问题,但它会减少很多不必要的代码。ByteFile.ReadAllBytes
0赞 roy 7/27/2022
@John,如果我使用这样的代码,那么没有错误,这发生错误,因为我使用 ESC/P 代码格式SendFileToPrinter(printer, "C:\vDos\#LPT1.asc")
0赞 F0r3v3r-A-N00b 7/27/2022
假设 LPT1.asc 存在并且实际上位于 C:\vDOS 文件夹中,请将此 SendFileToPrinter(printer, (BoldOn + (“C:\vDos\#LPT1.asc” + BoldOff))) 更改为此 SendFileToPrinter(printer, “C:\vDos\LPT1.asc”)。
0赞 roy 7/27/2022
@F0r3v3r-A-N00b , .是的,没错,这是文件的实际位置“#LPT1。ASC”is actually located in C:\vDOS folder.
0赞 F0r3v3r-A-N00b 7/28/2022
您不能将文件的名称更改为其他名称吗?不要在文件名中输入 #,也不要将其命名为 LPT1。

答:

0赞 John 7/27/2022 #1

您将结果传递给方法的参数,但使用该参数的唯一位置是:(BoldOn + ("C:\vDos\#LPT1.asc" + BoldOff))szFileName

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

要添加的前缀和后缀正在创建一个值,就构造函数而言,该值不是有效的文件路径,因此会出现异常。您需要将有效的文件路径传递给该构造函数。FileStream

似乎应该以某种方式将前缀和后缀传递给打印机,但您没有这样做。我的猜测是您需要将该前缀和后缀添加到文件中的实际数据中,而不是文件路径,例如

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()

看来我需要详细拼写出来,所以这里是你修改的原始代码,以纳入我上面解释的内容:

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

请注意,这只是一个有根据的猜测,您需要将这些打印机命令与文件内容相结合。由您来确认这一点,如果不是这种情况,请找出所需的内容。

评论

0赞 roy 7/27/2022
感谢您的回答,但有一个错误。 在代码行中Value of type '1-dimensional array of Byte' cannot be converted to 'String'.SendFileToPrinter(printer, dataToPrint)
0赞 John 7/27/2022
@user19541848,让我们戴上思考的帽子,好吗?你已经有一个接受数组的方法,那么为什么要调用和传递数组呢?该方法的全部意义在于,向其传递一个文件路径,然后从该文件中读取数据。也许这样做。SendBytesToPrinterByteSendFileToPrinterByteSendFileToPrinter
0赞 roy 7/27/2022
Argument not specified for parameter 'dwCount' of 'Public Shared Function SendBytesToPrinter(szPrinterName As String, pBytes As System.IntPtr, dwCount As Integer) As Boolean'.我有 2 个错误.我使用这样的代码Value of type '1-dimensional array of Byte' cannot be converted to 'System.IntPtr'.SendBytesToPrinter(printer, dataToPrint)
0赞 John 7/28/2022
@user19541848,请参阅我的编辑。
0赞 roy 7/28/2022
感谢您的回复,重要的是您的代码可以读取文件的内容。大多数情况下,我正在寻找运行打印机命令的解决方案