简体   繁体   English

File.Copy目标文件是目录,而不是文件。

[英]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. 我可能没有正确地做到这一点,浏览MSDN库对我没什么帮助。 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. 我目前得到的错误是Environment.SpecialFolder.MyDocuments类指示“我的文档”是文件夹,而不是文件。 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 这在文档中明确指出: 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:\\" ) (而不是"C:\\"

As MSDN States 作为MSDN国家

destFileName Type: System.String The name of the destination file. destFileName类型:System.String目标文件的名称。 This cannot be a directory. 这不能是目录。

So add the name of the file to the destination path. 因此,请将文件名添加到目标路径。

File.Copy() File.Copy()

Try this 试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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