473,396 Members | 1,760 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,396 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 1626
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.