473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File monitor/copy code doesn't catch everything

All,
This is sort of a continuation of a previous post of mine. The code below
basically reads a registry key to get a path to a folder and it watches for
files created in that folder (only created). It also reads another registry
key for another path which is a destination path. When a file shows up, it
copies it off.

It actually works well . . . For small duty stuff but if for example I were
to copy in multiple small files simulteneously into the source folder, it
only copies the first one it sees. It sorta peeters out after that, and I
don't think it continues on copying.

Also, if I drop a large file -- 500MB or higher -- into the folder, it
starts copying, but if I drop another file in the folder during this time,
it doesn't pick it up. I don't think it even finished copying the large
file.

Just wanted to see if y'all could give me some direction/advice.

TIA,
Pair

Imports System.ServiceProcess

Imports System.Threading

Public Class Service1

Inherits System.ServiceProcess.ServiceBase

Dim DestDirectory As String

Dim WatchDirectory As String

Private Watchservice As New Thread(New
System.Threading.ThreadStart(AddressOf StartMonitor))

#Region " Component Designer generated code "

Public Sub New()

MyBase.New()

' This call is required by the Component Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call

End Sub

'UserService 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

' The main entry point for the process

<MTAThread()_

Shared Sub Main()

Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add

' another service to this process, change the following line to

' create a second service object. For example,

'

' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1,
New MySecondUserService}

'

ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}

System.ServiceProcess.ServiceBase.Run(ServicesToRu n)

End Sub

'Required by the Component Designer

Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer

' It can be modified using the Component Designer.

' Do not modify it using the code editor.

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

components = New System.ComponentModel.Container()

Me.ServiceName = "Service1"

End Sub

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)

' Start the thread.

Watchservice.Start()

End Sub

Protected Overrides Sub OnStop()

' Add code here to perform any tear-down necessary to stop your service.

Watchservice.Abort()

End Sub

Private Sub StartMonitor()

Do

WatchDirectory =
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\ Software\CopyMon",
"Source", Nothing)

DestDirectory =
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\ Software\CopyMon",
"Destination", Nothing)

Dim watcher As New System.IO.FileSystemWatcher(WatchDirectory)

Dim result

AddHandler watcher.Created, AddressOf logchange

'Dim SourceFileNameObject As IO.WaitForChangedResult

'Dim SourceFileName As String

'SourceFileName = SourceFileNameObject.Name

'MsgBox(SourceFileName)

result = watcher.WaitForChanged(System.IO.WatcherChangeType s.Created)

Loop

End Sub

Private Sub logchange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)

Dim SourceFilePath As String

Dim SourceFileName As String

Dim DestPath As String

If e.ChangeType = IO.WatcherChangeTypes.Created Then

SourceFilePath = e.FullPath

SourceFileName = e.Name

'MsgBox(SourceFilePath)

'MsgBox(DestDirectory & "\" & SourceFileName)

DestPath = DestDirectory & "\" & SourceFileName

My.Computer.FileSystem.CopyFile(SourceFilePath, DestPath)

End If

End Sub

End Class
Mar 23 '07 #1
2 2781
On Mar 23, 3:39 pm, "TwistedPair" <twistedp...@mail.comwrote:
All,
This is sort of a continuation of a previous post of mine. The code below
basically reads a registry key to get a path to a folder and it watches for
files created in that folder (only created). It also reads another registry
key for another path which is a destination path. When a file shows up, it
copies it off.

It actually works well . . . For small duty stuff but if for example I were
to copy in multiple small files simulteneously into the source folder, it
only copies the first one it sees. It sorta peeters out after that, and I
don't think it continues on copying.
What you see as 'simultaneous' copying of several files, to the
computer is probably not simultaneous at all. It probably sees the
creation of the first file and then kicks off it's processing, not
getting an event for the other files. One possible way of getting
around this is when you see the create event, get a list of ALL files
that match your criteria and then copy them all.
>
Also, if I drop a large file -- 500MB or higher -- into the folder, it
starts copying, but if I drop another file in the folder during this time,
it doesn't pick it up. I don't think it even finished copying the large
file.
Same as above, once the 500Mb file appears, the file system watcher
fires the created event and your code launches into the long process
of copying the file. Other files copied after that, don't get
events.

One method would be to simply have your FileSystemWatcher add the
filename to a queue or list and then go back to watching for files.
The copying process could be kicked off using a background thread
which continually monitors the queue and if a filename is found there,
copies it. So you would have two separate processes. One which uses
the FileSystemWatcher to monitor for files and insert their names into
a queue and another to watch the queue and do the actual copying.

Hopefully this will give you some ideas.

Chris

Mar 23 '07 #2
Yes it does! I was wondering if that was indeed the best way to handle it,
and now I suppose I have confirmation. Now all I have to do is figure out
how. Thank you for your pointers, and any others would be greatly
appreciated as well!

"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@y66g2000hsf.googlegro ups.com...
On Mar 23, 3:39 pm, "TwistedPair" <twistedp...@mail.comwrote:
>All,
This is sort of a continuation of a previous post of mine. The code
below
basically reads a registry key to get a path to a folder and it watches
for
files created in that folder (only created). It also reads another
registry
key for another path which is a destination path. When a file shows up,
it
copies it off.

It actually works well . . . For small duty stuff but if for example I
were
to copy in multiple small files simulteneously into the source folder, it
only copies the first one it sees. It sorta peeters out after that, and
I
don't think it continues on copying.

What you see as 'simultaneous' copying of several files, to the
computer is probably not simultaneous at all. It probably sees the
creation of the first file and then kicks off it's processing, not
getting an event for the other files. One possible way of getting
around this is when you see the create event, get a list of ALL files
that match your criteria and then copy them all.
>>
Also, if I drop a large file -- 500MB or higher -- into the folder, it
starts copying, but if I drop another file in the folder during this
time,
it doesn't pick it up. I don't think it even finished copying the large
file.

Same as above, once the 500Mb file appears, the file system watcher
fires the created event and your code launches into the long process
of copying the file. Other files copied after that, don't get
events.

One method would be to simply have your FileSystemWatcher add the
filename to a queue or list and then go back to watching for files.
The copying process could be kicked off using a background thread
which continually monitors the queue and if a filename is found there,
copies it. So you would have two separate processes. One which uses
the FileSystemWatcher to monitor for files and insert their names into
a queue and another to watch the queue and do the actual copying.

Hopefully this will give you some ideas.

Chris

Mar 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3188
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
5
1113
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
9
715
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
4
15390
by: Shannon Hardin | last post by:
I'm trying to build a routine that will take a specified file from the user's local hard drive, and copy it to a remote server via WebDAV. I've tried using both HttpWebRequest and FileWebRequest,...
8
9699
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
6
2114
by: Clark Sann | last post by:
Can someone help me understand what object should be used as the lock object? I've seen some programs that use Monitor.Enter(Me). Then, in those same programs, they sometimes use another object. ...
17
2676
by: spentun | last post by:
How can I moitor file which is executed by user or program in window. I need to write a code to monitor file. When user clicks the file(.exe) or is called by program, the name of file will be...
8
6692
by: Philip Wagenaar | last post by:
I need to send printjobs to a printqueue under diffrent usernames. The printsoftware on the queue is not very 'secure' so I can create a user on 1 system and send a printjob under it's name to the...
1
2723
by: Dan | last post by:
I have an application that I want to use for copying files. My goal is to copy a files, if a file is in use or not accessible because of security reasons I want to make note of that file then...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.