2017-03-12 7 views
0

新しい宛先にファイルをコピーしようとすると、エラーが発生します。新しい宛先にファイルをコピーする

「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

+0

あなたは[Server.MapPathの](HTTPとの実際のパスに仮想パスをマップする必要があります:// stackoverflowの.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath)。 –

+0

@NicoSchertlerは答えとしてそれを追加し、うまくいきました –

答えて

0

uがこれにこのコードを変更してくださいすることができます

 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); 
関連する問題