将文件复制到新目标

时间:2017-03-12 18:56:54

标签: asp.net-mvc file

当我尝试将文件复制到新目的地时,我收到错误;

  

发生了'System.IO.DirectoryNotFoundException'类型的异常   在mscorlib.dll中但未在用户代码中处理

     

其他信息:无法找到路径的一部分   '〜/内容/ fileHistory / Resume.pdf'。

然而文件就在那里。

这是我的代码,找到了正确的fileName;

            Random randNums = new Random();

            string fileName = documentUps.Attachment ;
            string newFileName = fileName + randNums;
            string sourcePath = "~/Content/fileHistory/";
            string targetPath = "~/Content/reviseFiles/";

            // Use Path class to manipulate file and directory paths.
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, newFileName);

            System.IO.File.Copy(sourceFile, destFile, true);

https://gyazo.com/2d402824178738c6fb0f873904db6cdf

1 个答案:

答案 0 :(得分:0)

请将此代码更改为:

        Random randNums = new Random();

        string fileName = documentUps.Attachment;
        string newFileName = fileName + randNums;
        string sourcePath = "~/Content/fileHistory/";
        string targetPath = "~/Content/reviseFiles/";

        // Use Path class to manipulate file and directory paths.
        string sourceFile = Server.MapPath(System.IO.Path.Combine(sourcePath, fileName));
        string destFile = Server.MapPath(System.IO.Path.Combine(targetPath, newFileName));

        System.IO.File.Copy(sourceFile, destFile, true);