WebClient Download - “Access to the path 'c:\\

2019-03-03 08:59发布

问题:

I am attempting to download a file from another IIS site on my local machine. I have my main website that is trying to download from another public site that contains a few files that the user will be able to download.

[HttpGet]
public ActionResult DownloadMyPrintManagerInstaller()
{
    bool success;
    try
    {
        using (var client = new WebClient())
        {
            client.DownloadFile(new Uri("http://localhost:182//MyPrintInstaller.exe"), "MyPrintManager.exe");
        }
        success = true;
    }
    catch (Exception)
    {
        success = false;
    }

    return Json(new { Success = success }, JsonRequestBehavior.AllowGet);
}

For some reason, it is attempting to download the file from C:\windows\system32\inetsrv\MyPrintManager.exe? Does anyone know how I can avoid it from pointing to that directory? Do I need to modify my code or my IIS configuration?

My virtual directory for this site is a folder sitting on my C: drive, and the file I actually want to download is in that directory.

回答1:

No, it's attempting to save the file to C:\windows\system32\inetsrv\MyPrintManager.exe.

That's because C:\windows\system32\inetsrv is the working directory for your process, and you've just given a relative filename.

Specify an absolute filename which says exactly where you want the file to be stored, and it should be fine.



回答2:

WebClient web = new WebClient();
string url = "http://.../FILENAME.jpg";
web.DownloadFile(new Uri(url), "C:/FILENAME.jpg");