Reputation: 431
I'm trying to copy a file to another directory but I'm getting this error
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Documents and Settings\(my username)\Desktop\Source_Folder\File_1.xlsx'.
But File_1.xlsx exists in the folder Source_Folder which exists on my Desktop. So why am I getting this error?
Here is my code
Dim path_orig As String = "C:\Documents and Settings\(my username)\Desktop\Source_Folder\"
Dim oldFile As String = System.IO.Path.Combine(path_orig, filename)
Dim path_new As String = Server.MapPath("~") & "\Destination_Folder\"
Dim newFile As String = System.IO.Path.Combine(path_new, filename)
File.Copy(oldFile, newFile, True)
*filename is a variable which in this case is "File_1.xlsx"
Upvotes: 0
Views: 8245
Reputation: 74909
Don't hard code Documents and Settings
. It will change by OS. Use this instead:
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Source_Folder")
Also if you're running this in a web application you're likely running into permissions problems. Change the location where you're storing files and/or change the user the app pool is running under and/or grant the app pool user rights to the folder.
Upvotes: 3