0

I am trying to download a file using webclient method DownloadFile But its giving me error that

 The process cannot access the file '...\d915877c-cb7c-4eeb-97d8-41d49b75aa27.docx' because it is being used by another process.

But when I open the file by clicking it, it opening.

There are same question requesting same info but none are accepted answers.

any help will be appreciated

It may be oS that is not letting file go. Whatever it is but after searching a lot I am unable to find solution

Here is the code to create a file

  Document d = new Document();
  d.Save(HttpContext.Current.Server.MapPath(@"Invoice\" + iname + ".docx"));

I am using aspose word dll

and following way I am accessing it

 using (var client = new System.Net.WebClient())
        {

            client.UseDefaultCredentials = true;
            client.DownloadFile(Server.MapPath("invoice/" + Request.QueryString["id"].ToString() + ".docx"),Server.MapPath("invoice/" +Request.QueryString["id"].ToString() + ".docx"));
            client.Dispose();
        }

and BTW its giving same error even to files that are not creaated using code.

user786
  • 3,902
  • 4
  • 40
  • 72

1 Answers1

1

Give a different path where to save the download file which is different from the download source path. If you want to replace the file do it after disposing the webclient by using File.replace() method.

string downloadPath = "Your download path";      
string destinationPath = "the path where the file should be saved";`//this should be different from "download path"                   
 File.Download(downloadPath,destinationPath);
Isuru
  • 430
  • 5
  • 21
  • the object does not have either close or dispose method, so I am assigning the object value null – user786 Jan 27 '16 at 10:57
  • and BTW its giving same error even to files that are not creaated using code. – user786 Jan 27 '16 at 10:58
  • Ok I get your problem. You are trying to download the file to the same location as the download source path. `client.DownloadFile(Server.MapPath("invoice/" + Request.QueryString["id"].ToString() + ".docx"),""Differentpath"))`; – Isuru Jan 27 '16 at 11:08
  • `string downloadPath = "Your download path";` `string destinationPath = "the path where the file should be saved";` //this should be different from "download path" `File.Download(downloadPath,destinationPath);` – Isuru Jan 27 '16 at 11:24