954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Copy a file in VB.net

Hello.

I want to copy a file from a UNC path (ex: \\comname\dirname\filename.txt) to a local destination (c:\outputdir)

I have no compile errors, but when I run it, it throws an exception (System.IO.DirectoryNotFoundException). I'm absolutely sure the destination existst (it's referencing the destination directory). I've tried doing it with system.io.file.copy(source,dest) and my.computer.filesystem.copyfile(source,dest). Same result either way.

This should be easy, what am I missing??
Thanks

napkinbob
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Post the code you are using along with the evaluated value of source and dest for File.Copy() . I have a number of apps that copy from UNC paths and they work OK, except for when the remote machine goes down. Then it throws an IOException . Does your code fail every time or just sometimes? It might be a sharing/permission issue.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Here's the code I'm using:

Dim term As Integer = 1
Dim storenum As String = 2234
Dim sourcefile As String = ("\\t" & term.ToString & "\system3\" & "sys3error.log")
Dim destfile As String = (destination.Text & storenum & "\" & DateTimePicker1.Value.ToString("MMMM") & "-" & DateTimePicker1.Value.ToString("dd") & "\T" & term.ToString & "\Error Log\")
        System.IO.File.Copy(sourcefile, destfile)


destination.text contains a user specified output dir, in this case c:\output\
This produces a value of \\t1\system3\sys3error.log for sourcefile and c:\output\2234\January-14\T1\Error Log\ for destfile

when i run it, it thows the exception System.IO.DirectoryNotFoundException, could not find part of path 'c:\output\2234\January-14\T1\Error Log\'

I even commented out the destfile variable and change it to c:\testdir\ (which I created first). It throws the same error for that path, every time.

There's a different part of the code that checked which machines are on the network to avoid a network path not found exception, but for this example I'm only talking to \\T1\, and I will probably put a catch in if the file doesn't exist for some reason, but I just want to get the copy part working first.

I can navigate and have read/write access to both source and dest folders.

Thanks

napkinbob
Newbie Poster
10 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

your destfile needs to be a full file path, not just the destination directory. Try c:\output\2234\January-14\T1\Error Log\destination.text for your outfile (i added the filename at the end)

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

im doing a programme how to copy the files that had been work today or there last modifdate=today but except a certain type of file exemple .tif

fatiza
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

im doing a programme how to copy the files that had been work today or there last modifdate=today but except a certain type of file exemple .tif [ICODE]

fatiza
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: