copy and then Delete a file.

P

Petter L

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.
 
C

Cor Ligthert

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
 
P

Petter L

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.
 
C

Cor Ligthert

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/d...ef/html/frlrfsystemiofileclassdeletetopic.asp

I hope this helps somehow?

Cor
 
C

Chris

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
 

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