WebClient DownloadFile - Access denied or could not find part of the path

19,172

Got your issue into my head. When you have used webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), @"C:\web\test.html");, then make sure that you have directory web in C:\. I mean make sure you have C:\web in your file system. Else it will throw error. Because it needs directory to exist, so that it can just create new file.

When you did webclient.DownloadFile(@"D:\web\mytestwebsite.com\www\temp\test.html", "test.html");, it tried to create file in current execution file scope i.e. C:\Windows\SysWOW64\inetsrv\ and there it tried to create file, due to access permission, it is not able to create file there.

Solution, try to use following code, it should create file in D:\web.

webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), @"D:\web\test.html");

EDIT

Disclaimer You are doing it wrong ( if i am not clear about requirement ), DownloadFile is to download an url to server location, there is now point for download a local file and saving that to again local location, it is like save as file to any other location.

EDIT

To allow end user to download file use following code.

Response.Clear();
Response.ContentType = "text/html";
Response.AddHeader("Content-Disposition", "attachment;filename=a.html"); // replace a.html with your filename
Response.WriteFile(@"D:\webfile\a.html"); //use your file path here.
Response.Flush();
Response.End();
Share:
19,172
Greg
Author by

Greg

Updated on June 07, 2022

Comments

  • Greg
    Greg almost 2 years

    I am trying to set a button so that the user can download a file saved on the server.

    protected void btnDownloadHtmlFile_Click(object sender, EventArgs e)
    {
        string path = @"D:\web\mytestwebsite.com\www\temp\test.html";
        if (!File.Exists(path))
        {
            File.Create(path);
        }
    
        TextWriter tw = new StreamWriter(path);
        tw.WriteLine("<head></head><body>test</body>");
        tw.Close();
    
        WebClient webclient = new WebClient();
        webclient.DownloadFile(@"D:\web\mytestwebsite.com\www\temp\test.html", @"C:\web\test.html");
    }
    

    This result in Could not find a part of the path 'C:\web\test.html'. Same if I change for

     webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), @"C:\web\test.html");
    

    If I change for

    webclient.DownloadFile(@"D:\web\mytestwebsite.com\www\temp\test.html", "test.html");
    

    or

    webclient.DownloadFile(new Uri("http://mytestwebsite.com/temp/test.html"), "test.html");
    

    I get Access to the path 'C:\Windows\SysWOW64\inetsrv\test.html' is denied.

    Finally I went to the folder C:\Windows\SysWOW64\inetsrv to give permissions to NETWORK SERVICE but it says access denied. I am logged as a Admin on the server.

    I read a few posts on this but nothing seems to work for or I missed something.

    What is right way to use WebcClient.DownloadFile?