By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,681 Members | 1,864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,681 IT Pros & Developers. It's quick & easy.

copy and then Delete a file.

P: n/a
I getting the following message when using Filecopy command
An unhandled exception of type 'System.NullReferenceException' occurred in
Unknown Module.

Additional information: Object reference not set to an instance of an
object.

If I break then I get "There is no source code avaliable for the current
location.
If I Continue I get the following

An unhandled exception of type 'System.UnauthorizedAccessException' occurred
in microsoft.visualbasic.dll

Additional information: Access to the path "c:\test\AdshelAfter850-51.jpg"
is denied.

I have not the file loaded in any programs or looking on that file location.
I have full admin rights localy on my computer. The task is to watch for
files put in Directoy c:\A and then move it to c:\B at the moment.

Have someone any clue to what is wrong her ?

Petter L.
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Petter,

A nullreference is almost forever a non instanced object.
However the rest probably nobody can see without a little piece of code.

I hope this starts helps?

Cor
Jul 21 '05 #2

P: n/a
Here is a pice of the code
Private m_WatchDirectory As String

Private WithEvents m_FileSystemWatcher As FileSystemWatcher

Dim log As New Log

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

lstFiles.Items.Add(Now.ToString() & " Starting")

readReg()

log.lstFiles.Items.Add(Now.ToString() & " Init " & LookPath)

log.lstFiles.Items.Add(Now.ToString() & " Init " & ToPath)

' Get the path to the directory we will watch.

m_WatchDirectory = LookPath 'Application.StartupPath

' m_WatchDirectory = m_WatchDirectory.Substring(0,
m_WatchDirectory.LastIndexOf("\"))

'm_WatchDirectory &= LookPath '"\Files"

' Make the FileSystemWatcher.

m_FileSystemWatcher = New FileSystemWatcher(m_WatchDirectory, "*.*") '
"*.txt")

m_FileSystemWatcher.NotifyFilter = 0

m_FileSystemWatcher.NotifyFilter = m_FileSystemWatcher.NotifyFilter Or
NotifyFilters.FileName

m_FileSystemWatcher.EnableRaisingEvents = True

m_FileSystemWatcher.IncludeSubdirectories = alldirs

' Process any files that already exist.

MsgBox(m_WatchDirectory)

ProcessExistingFiles(m_WatchDirectory)
End Sub

Private Sub ProcessExistingFiles(ByVal directory_name As String)

Dim dir_info As New DirectoryInfo(directory_name)

Dim file_infos As FileInfo() = dir_info.GetFiles()

For Each fi As FileInfo In file_infos

ProcessFile(fi.FullName, fi.Name)

Next fi

End Sub

Private Sub ProcessFile(ByVal file_name As String, ByVal shName As String)

log.lstFiles.Items.Add(Now.ToString() & " Processed " & file_name)

If shName = "\Thumbs.db" Then ' = LCase(file_name) Then

Exit Sub

End If

MsgBox(file_name & " " & ToPath & "\" & shName)

FileCopy(file_name, ToPath & "\" & shName)

Kill(file_name)

End Sub

I also have the following in a module file

Public LookPath, ToPath As String

Public AllDirs As Boolean

Petter L.
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
Petter,

A nullreference is almost forever a non instanced object.
However the rest probably nobody can see without a little piece of code.

I hope this starts helps?

Cor

Jul 21 '05 #3

P: n/a
Peter,

When you want to give us some code to help you, than please make it
readable, all rows which are deleted with a ', makes it for others even more
unreadable what your code is now.

And than past it fist in a notebook, and than copy it back before you paste
it in your message.

When I look at your code am I directly thinking why is Peter not using the
File class to do things as deleting files.

http://msdn.microsoft.com/library/de...eletetopic.asp

I hope this helps somehow?

Cor
"Petter L" <petter at ho**@hotmail.com>

Here is a pice of the code
Private m_WatchDirectory As String

Private WithEvents m_FileSystemWatcher As FileSystemWatcher

Dim log As New Log

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

lstFiles.Items.Add(Now.ToString() & " Starting")

readReg()

log.lstFiles.Items.Add(Now.ToString() & " Init " & LookPath)

log.lstFiles.Items.Add(Now.ToString() & " Init " & ToPath)

' Get the path to the directory we will watch.

m_WatchDirectory = LookPath 'Application.StartupPath

' m_WatchDirectory = m_WatchDirectory.Substring(0,
m_WatchDirectory.LastIndexOf("\"))

'm_WatchDirectory &= LookPath '"\Files"

' Make the FileSystemWatcher.

m_FileSystemWatcher = New FileSystemWatcher(m_WatchDirectory, "*.*") '
"*.txt")

m_FileSystemWatcher.NotifyFilter = 0

m_FileSystemWatcher.NotifyFilter = m_FileSystemWatcher.NotifyFilter Or
NotifyFilters.FileName

m_FileSystemWatcher.EnableRaisingEvents = True

m_FileSystemWatcher.IncludeSubdirectories = alldirs

' Process any files that already exist.

MsgBox(m_WatchDirectory)

ProcessExistingFiles(m_WatchDirectory)
End Sub

Private Sub ProcessExistingFiles(ByVal directory_name As String)

Dim dir_info As New DirectoryInfo(directory_name)

Dim file_infos As FileInfo() = dir_info.GetFiles()

For Each fi As FileInfo In file_infos

ProcessFile(fi.FullName, fi.Name)

Next fi

End Sub

Private Sub ProcessFile(ByVal file_name As String, ByVal shName As String)

log.lstFiles.Items.Add(Now.ToString() & " Processed " & file_name)

If shName = "\Thumbs.db" Then ' = LCase(file_name) Then

Exit Sub

End If

MsgBox(file_name & " " & ToPath & "\" & shName)

FileCopy(file_name, ToPath & "\" & shName)

Kill(file_name)

End Sub

I also have the following in a module file

Public LookPath, ToPath As String

Public AllDirs As Boolean

Petter L.
"Cor Ligthert" <no************@planet.nl> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
Petter,

A nullreference is almost forever a non instanced object.
However the rest probably nobody can see without a little piece of code.

I hope this starts helps?

Cor


Jul 21 '05 #4

P: n/a
Is it possible that the process that is writing the file is not
finished when the FileSystemWatcher attempts to move the file?

You might try opening the file in exclusive mode first and if that
succeeds, then try moving the file. If that fails, wait a few seconds
and try again.

Just a thought

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.