【问题标题】:System.IO.IOExceptionSystem.IO.IOException
【发布时间】:2016-11-30 19:38:55
【问题描述】:

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

我使用的代码是:

File.Copy(srcPath, landscapebox.Text, true);

srcPath 是一个临时文件夹

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

"C:\Users\####\Folder\Folder"

但是我得到了:

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.

帮助!我不知道我做错了什么!

【问题讨论】:

    标签: c# windows file


    【解决方案1】:

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

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

    File.Copy(srcPath, 
        Path.Combine(landscapebox.Text, Path.GetFileName(srcPath)), true);
    

    【讨论】:

    • 哦。好尴尬
    猜你喜欢
    • 1970-01-01
    • 2013-07-15
    • 2020-12-27
    • 2018-03-06
    • 1970-01-01
    • 2011-11-22
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多