简体   繁体   中英

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

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);
}

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:\\" )

As MSDN States

destFileName Type: System.String The name of the destination file. This cannot be a directory.

So add the name of the file to the destination path.

File.Copy()

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM