Imports System.IO
...
Dim sw as New StreamWriter("lpt1")
sw.WriteLine("Hola, mundo")
sw.Close()
FileStream will not open win32 devices such as disk partitions and tape
drives. avoid use of "\\.\" in the path
como locorrijo?
JRLD
"Alberto Poblacion" <earthling-quitae...@poblacion.org>
escribió en el mensaje news:%23JcI8nh...@TK2MSFTNGP03.phx.gbl...
"Jesus Ramon Lopez Dominguez" <jrlo...@hotmail.com> escribió en el mensaje news:uXi5EehS...@TK2MSFTNGP05.phx.gbl...
"Jesus Ramon Lopez Dominguez" <jrlo...@hotmail.com> escribió en el mensaje news:OdimfLkS...@TK2MSFTNGP04.phx.gbl...
JRLD
"Alberto Poblacion" <earthling-quitae...@poblacion.org>
escribió en el mensaje news:%23JcI8nh...@TK2MSFTNGP03.phx.gbl...
No. Acabo de probarlo, y funciona con ficheros normales pero no funciona
con "lpt1" ni con "lpt1:" ni con "lpt1.dos". Me temo que no va a haber mas
remedio que usar P/Invoke para llamar a las APIs de Windows, tales como
CreateFile. En el primero de los enlaces que te indico a continuación viene
la forma de declarar las llamadas en VB, y el texto indica que el "handle"
que te devuelve el CreateFile se puede usar en .Net para inicializar un
FileStream, y menciona expresamente que esto vale para abrir "lpt1:".
http://www.pinvoke.net/default.aspx/kernel32/CreateFile.html
http://msdn2.microsoft.com/en-us/library/aa363858.aspx
Imports System
Imports Microsoft.VisualBasic
Imports System.IO
Imports Microsoft.Win32
Public Class Form1
Public Const GENERIC_WRITE = &H40000000
Public Const OPEN_EXISTING = 3
Public Const FILE_SHARE_WRITE = &H2
Public LPTPORT As String
Public hPort As Integer, hPortP As IntPtr
Public retval As Integer
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, 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 Integer) As Integer
Public Structure SECURITY_ATTRIBUTES
Private nLength As Integer
Private lpSecurityDescriptor As Integer
Private bInheritHandle As Integer
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SA As SECURITY_ATTRIBUTES
Dim outFile As FileStream
LPTPORT = "LPT1"
hPort = CreateFile(LPTPORT, GENERIC_WRITE, FILE_SHARE_WRITE, SA, OPEN_EXISTING, 0, 0)
hPortP = New IntPtr(hPort) 'convert Integer to IntPtr
Dim Safe As New Microsoft.Win32.SafeHandles.SafeFileHandle(hPortP, True)
outFile = New System.IO.FileStream(Safe, IO.FileAccess.Write)
Dim fileWriter As New StreamWriter(outFile)
fileWriter.WriteLine(Chr(27) & Chr(112) & 0) 'abrir cajón
fileWriter.WriteLine(" " & " Empresa")
fileWriter.WriteLine(" " & "Articulo " & " Precio " & " Cantidad " & " Total ")
fileWriter.WriteLine(" " & "---------------------------------------")
fileWriter.Close()
End Sub
End Class
Fíjate que tienes el CreateFile declarado como "As Integer", y que tal
como te advierten en
http://www.pinvoke.net/default.aspx/kernel32/CreateFile.html, eso solo
funciona con VB2003. Con el 2005 hay que usar un SafeFileHandle en lugar del
Integer.