Click here to Skip to main content
15,916,601 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I seem to be having problems getting this code to work

C#
Dim directory As New DirectoryInfo("C:\test\archive\")
        Dim files As FileInfo() = directory.GetFiles()

        For Each file In files

            If (file.LastAccessTime < DateTime.Now.AddDays(-1)) Then
                Dim fp As String
                fp = file.FullName
                System.IO.File.Move(fp, "C:\test\delete\")
            End If
        Next


I can't seem to find why, but it is hitting a first chance exception in mscorlib.dll

What I have tried:

I have tried a few different ways to try and shift the files, but it just doesn't want to.

What I am trying to do is very basic - if the file within the directory is older than one day, shift it to another directory.

I've been looking at a few different forums to try and find different ways to do it, but they all do the same thing. I've tried just using the file.moveto without converting the path to a string and it comes up with the same issue

while debugging, it picks up the correct full path and name for the file, and the directory being copied to is available.
Posted
Updated 24-May-16 0:52am
Comments
CHill60 24-May-16 6:01am    
What is the rest of the error message?
Is the source file read-only?
Does the target file already exist?
By "available" do you mean exists AND is not read-only AND your account is able to write to that folder?
Mendaharin 24-May-16 6:34am    
Ahh - perhaps the file I want to shift is locked during the for each loop?
Cant see anything saying anything is read only.
file doesn't exist in destination directory.

I've tried running vs as admin just in case but comes with the same error.
I can't seem to see much of an error message, just that it's a first exception in mscorlib.dll - this is while debugging.

I don't have a try catch in it. Haven't learnt how to use them as yet.
Mendaharin 24-May-16 6:35am    
from your answer, I'm guessing it looks like it should work?
CHill60 24-May-16 6:41am    
I can't see any reason for it not to work ... I had to step away but I'm just about to give it a try on my system.
Mendaharin 24-May-16 6:46am    
Mate I've just copied that code into a blank project - it's coming up saying the filename already exists... My code is screwy :(
thanks for your help! I should have thought of doing that myself...

1 solution

I had made a silly mistake!
I still need to have the name of the file that I want to copy.

It should have been as follows
C#
Dim directory As New DirectoryInfo("C:\test\archive\")
        Dim files As FileInfo() = directory.GetFiles()

        For Each file In files

            If (file.LastAccessTime < DateTime.Now.AddDays(-1)) Then
                Dim fp As String
                fp = file.FullName
                System.IO.File.Move(fp, "C:\test\delete\" & file.Name & "")
            End If
        Next
 
Share this answer
 
Comments
CHill60 24-May-16 6:57am    
Good call ... I didn't spot that! :blush:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900