1

I am currently learning uploading / downloading / deleting files in C# asp.net. I figured out how to delete every file in a folder with code like this:

protected void DeleteAllFiles(object sender, EventArgs e)
{
    System.IO.DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Output"));
    
    foreach (FileInfo file in di.GetFiles())
    {
        file.Delete();
    }

    foreach (DirectoryInfo dir in di.GetDirectories())
    {
        dir.Delete(true);
    }

    Response.Redirect("~/Outputs.aspx?ReturnPath=" + Server.UrlEncode(Request.Url.ToString()));
}

But I can't find anything on how to download all files in a directory. I figured out how to download individual files but I am having trouble with a button that downloads all files in a directory. Surely there is an easy way to do this? I can't find it anywhere else so this is probably a dumb question to ask but any help is appreciated.

2
  • 2
    So think of it not as 'downloading' files, but 'copying' files and you'll be able to find what you're looking for. The answer is simple and you want to learn, so I suggest you take a look at this documentation and the answer should become clear learn.microsoft.com/en-us/dotnet/api/… Commented Feb 8, 2022 at 17:05
  • 1
    If you actually want to download a file from a website, that's a whole other thing including sending web requests, which it's probably worth looking at a guide on, this is probably one of the more simple guides, but learning more about HttpRequests in general will help you jonathancrozier.com/blog/how-to-download-files-using-c-sharp Commented Feb 8, 2022 at 17:10

1 Answer 1

1

It's not possible to send multiple files in a single HTTP request. However, you can create a zip archive of multiple files and send that instead. See this answer for an example.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir, I have learned a lot and I did not know this. I figured out how to make a zip file and download that instead. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.