Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.Copy target file is a directory, not a file.

Tags:

c#

I am probably not doing this correctly, and browsing the MSDN library hasn't helped me much. I am trying to copy my database from my project folder to another location. I initially tried the desktop, and it stated the directory was not available. This is what I currently have.

private string currentDb = @"J:\Project\Project\HotelDB.accdb",
               backUpPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
               newFileName = @"\";

I call it with this method. The error I currently get is that the Environment.SpecialFolder.MyDocuments class indicates that 'My Documents' is a folder, not a file. This tells me that I'm doing this all wrong. Any guidance is appreciated.

public void backupDatabase()
{
    File.Copy(currentDb, backUpPath, true);
}
like image 250
user1729696 Avatar asked May 02 '13 07:05

user1729696


People also ask

What does file Copy do?

Definition. Copies an existing file to a new file.

How does file copy work in C#?

Copy method takes three parameters. First the original file with full path, second the file to be copied file name with the new path and third parameter that is optional that is used to overwrite an existing file. If third parameter is true, the Copy method will overwrite if file already exists.


1 Answers

You should add the filename to the target path. This is clearly specified in the documentation: http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx

The name of the destination file. This cannot be a directory or an existing file.

For example:

"J:\Project\Project\HotelDB.accdb"

Should go to:

"c:\HotelDB.accdb"

(And not "C:\")

like image 151
RvdK Avatar answered Oct 07 '22 09:10

RvdK