473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

copy and then Delete a file.

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
4 1624
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
3
by: News | last post by:
Is it possible to delete a file by copying it to the "bit bucket" or "null device"? Back in my youth when I live in VMS-land you could delete a file by copying it to NL: ========== I have...
0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
5
by: Hans Munich | last post by:
Szenario: User selects a XML File ("products.xml") to import newest Data. Data should replace Data in an existing Acess File which is connected via Dataadapter (daadaptFLR). Bevore the Existing...
25
by: Andy_Khosravi | last post by:
I just recently changed my database that I'm running from a monolithic DB to a split FE/BE. The front end resides on the client machine and the BE resides on a network drive. I'm experimenting with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.