delete file problem

A

apple

i try to print image file in a directory using PrintDocument. It will raise
printPage event to draw image to the printer. The file will be deleted
after print and the directory will be checked every second to get new file
inside it.
Where should i do the delete function? The way to print image is get from
msdn. Can tell me where can i refer to other better ways to print the
image file.
Thank you.

Public fileName As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub

Sub printImage(ByVal fileName As String)
Dim pd As New PrintDocument
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
End Sub

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As
PrintPageEventArgs)
' Draw a picture.
ev.Graphics.DrawImage(Image.FromFile(fileName),
ev.Graphics.VisibleClipBounds)
' Indicate that this is the last page to print.
ev.HasMorePages = False
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim dir As New DirectoryInfo("c:\dir")
Dim fi As FileInfo() = dir.GetFiles()
Dim fitemp As FileInfo

For Each fitemp In fi
fileName = fitemp.FullName
printImage(fileName)
Next
End Sub

___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 
M

Michael Proctor

Sorry dude,

can't help much with the printing image side of things, but I hope this is a
HUGE help to you. Instead of using a timer that checks every second, use the
filesystemwatch component. IT ROCKS.

It allows you to select a folder (including sub folders) and watch it and
raise events based on whats going on.

I have attached a small demo project, just add a form (leave it named as
Form1) then go into the code and replace all code with what is below, Run
(F5) then create a txt file in c:\, then open it and change it, rename it
and delete it, it will raise the 4 different events in the filewatch
compoment. It is a cool tool! :)

Regards,

Michael Proctor


Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents fsWatch As System.IO.FileSystemWatcher

Friend WithEvents Label1 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.fsWatch = New System.IO.FileSystemWatcher

Me.Label1 = New System.Windows.Forms.Label

CType(Me.fsWatch, System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'

'fsWatch

'

Me.fsWatch.EnableRaisingEvents = True

Me.fsWatch.Path = "c:\test\"

Me.fsWatch.SynchronizingObject = Me

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(176, 56)

Me.Label1.Name = "Label1"

Me.Label1.TabIndex = 0

Me.Label1.Text = "Label1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

CType(Me.fsWatch, System.ComponentModel.ISupportInitialize).EndInit()

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

fsWatch.Filter = "*.txt"

fsWatch.Path = "c:\"

fsWatch.EnableRaisingEvents = True

End Sub

Private Sub fsWatch_Changed(ByVal sender As System.Object, ByVal e As
System.IO.FileSystemEventArgs) Handles fsWatch.Changed

MsgBox("File: " & e.Name & ControlChars.CrLf & "Path: " & e.FullPath &
ControlChars.CrLf & ControlChars.CrLf & "Was Changed :|")

End Sub

Private Sub fsWatch_Created(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles fsWatch.Created

MsgBox("File: " & e.Name & ControlChars.CrLf & "Path: " & e.FullPath &
ControlChars.CrLf & ControlChars.CrLf & "Was Created :)")

End Sub

Private Sub fsWatch_Deleted(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles fsWatch.Deleted

MsgBox("File: " & e.Name & ControlChars.CrLf & "Path: " & e.FullPath &
ControlChars.CrLf & ControlChars.CrLf & "Was Deleted :(")

End Sub

Private Sub fsWatch_Renamed(ByVal sender As Object, ByVal e As
System.IO.RenamedEventArgs) Handles fsWatch.Renamed

MsgBox("File: " & e.Name & ControlChars.CrLf & "Path: " & e.FullPath &
ControlChars.CrLf & ControlChars.CrLf & "Was Renamed >:-|")

End Sub

Private Sub fsWatch_Error(ByVal sender As Object, ByVal e As
System.IO.ErrorEventArgs) Handles fsWatch.Error

MsgBox("Error: " & e.GetException.Message & " in source: " &
e.GetException.Source)

End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top