1

I want to download video file and save it to the project folder. Here is my current code for the video download and save it to a folder.

string savePath = Server.MapPath("DownloadedClip/Record1.mp4");
  //Delete existing file
  if (File.Exists(savePath))
   {
    File.Delete(savePath);
   }
  //Create a new file
  using (File.Create(savePath)) //Exception Occur here
  {
    using (WebClient wc = new WebClient())                                      
    wc.DownloadFile(url, savePath);
   }

When I run the project it throws an exception as "Could not find a part of the path." How can I solve the issue?

Appreciate your help with this.

Thank You.

priyanka
  • 23
  • 4
  • I'm going to guess you haven't created the directory you're trying to save to. – Retired Ninja Sep 22 '20 at 04:47
  • No, I have a directory as "DownloadedClip" in the project folder. – priyanka Sep 22 '20 at 04:54
  • That assumes your working directory is the project folder. It may not be. – Retired Ninja Sep 22 '20 at 04:58
  • I saw one thing that the folder will be deleted after executing if (File.Exists(savePath)) this line and it will not be added by File.Create(savePath) this line. – priyanka Sep 22 '20 at 05:02
  • How to handle this situation? To check the existing file and add a new one to the same folder. – priyanka Sep 22 '20 at 05:07
  • I add the line `string fullpath = Path.Combine(savePath, filename);` after savepath and before `if (File.Exists(savePath))` this line. then the issue: "Could not find a part of the path" is resolved. But it throws exception on `wc.DownloadFile(url, savePath);` this line. The exception is "The process cannot access the file because it is being used by another process." – priyanka Sep 22 '20 at 05:24
  • Hope this helps you out: https://stackoverflow.com/questions/13262548/delete-a-file-being-used-by-another-process/21137207#21137207 – शेखर Sep 22 '20 at 06:23
  • 1
    thank you @शेखर issue has been resolved. Made changes in code as below: `string filenameRecording = @"\Record1.mp4"; string fullpathRecording = Server.MapPath("DownloadedClip") + filenameRecording; //Delete existing file if (File.Exists(fullpathRecording)) { File.Delete(fullpathRecording); } //Create a new file using (var fileRecording=File.Create(fullpathRecording)) { fileRecording.Close(); using (WebClient wc = new WebClient()) wc.DownloadFile(url, fullpathRecording); }` – priyanka Sep 22 '20 at 08:49

0 Answers0