System.IO.IOException

Hyb*_*ker 0 c# windows file

我试图将文件从临时目录复制到用户选择的文件夹.我正在使用C#,我使用的文件夹是空的.

我正在使用的代码是:

File.Copy(srcPath, landscapebox.Text, true);
Run Code Online (Sandbox Code Playgroud)

srcPath 是一个临时文件夹

landscapebox是一个文本框,其中有一个目录输入.它应该看起来像:

"C:\Users\####\Folder\Folder"
Run Code Online (Sandbox Code Playgroud)

但相反,我得到:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The target file "C:\Users\###\Desktop\####\TestFolder" is a directory, not a file.
Run Code Online (Sandbox Code Playgroud)

救命!我不知道我做错了什么!

ror*_*.ap 6

这是因为File.Copy中的第二个参数是目标文件路径而不是目标文件夹路径.

您可以从输入文件夹构造目标文件名,如下所示:

File.Copy(srcPath, 
    Path.Combine(landscapebox.Text, Path.GetFileName(srcPath)), true);
Run Code Online (Sandbox Code Playgroud)