|
-
Jul 13th, 2003, 12:20 AM
#1
Thread Starter
Addicted Member
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
-
Jul 13th, 2003, 12:49 AM
#2
Why are you using the API to create the file instead of using the FileStream to create it?
VB Code:
Dim fs As New FileStream("C:\xyz.txt", FileMode.Create)
Dim sw As New StreamWriter(fs)
sw.WriteLine("Simple Text")
sw.Close()
'closing the streamwriter will close the file but
'you can still close on the filestream if you want
fs.Close()
There are also these FileModes:
OpenOrCreate and CreateNew
Last edited by Edneeis; Jul 13th, 2003 at 12:56 AM.
-
Jul 13th, 2003, 01:35 PM
#3
you could do it like this also , just shortned down slightly :
VB Code:
Dim sWrite As IO.StreamWriter = New IO.StreamWriter(New IO.FileStream("C:\im_a_text.txt", IO.FileMode.OpenOrCreate))
sWrite.Write("some text")
sWrite.Close()
or if you just wish to create a file of any type , eg: txt you can also do this :
VB Code:
Dim f As IO.File
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]
-
Jul 14th, 2003, 05:19 AM
#4
Thread Starter
Addicted Member
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.
-
Jul 14th, 2003, 06:42 AM
#5
Thread Starter
Addicted Member
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
-
Jul 16th, 2003, 02:56 PM
#6
Thread Starter
Addicted Member
-
Jul 16th, 2003, 03:50 PM
#7
Try using an IntPtr type instead of Integer.
-
Jul 17th, 2003, 12:01 PM
#8
Thread Starter
Addicted Member
FileStream/StreamWriter Problem [Resolved]
Thanks Edneeis
It is working fine and with also "LPT1:"
Thanks again.
-
Jan 16th, 2004, 04:46 PM
#9
This method might be what I'm looking for.
Anybody try it with a ps/pdf/print file?
Or would it just print the code within the file as text?
hmmmm.
-Lou
-
Feb 6th, 2004, 09:18 PM
#10
Frenzied Member
VB Code:
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)
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.
-
Feb 7th, 2004, 01:10 AM
#11
Frenzied Member
I managed to resolve this as follows:
Call WriteToPort("LPT1:", "The text that i want to output to LPT1 port")
The
VB 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
Public Sub WriteToPort(ByRef sPort As String, ByRef sText As String)
Dim FileHandle As Long
Dim FileHandleIntPtr As IntPtr
Dim FStream As FileStream
Dim SWriter As StreamWriter
FileHandle = Win32API.CreateFile(sPort, Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0)
FileHandleIntPtr = New IntPtr(FileHandle)
FStream = New FileStream(FileHandleIntPtr, FileAccess.Write, False)
FStream.Flush()
SWriter = New StreamWriter(FStream)
SWriter.WriteLine(sText)
FStream.Flush()
SWriter.Close()
FStream.Close()
Win32API.CloseHandle(FileHandle)
End Sub
-
Feb 18th, 2004, 01:34 PM
#12
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|