skip to Main Content

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

Answers


  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.

    Login or Signup to reply.
  2.         internal class User_Directory
            {
                public string directory_name;
                public List<Tuple<string, byte[]>> files_list = new List<Tuple<string, byte[]>>();
                public List<User_Directory> sub_directories = new List<User_Directory>();
            }
    
    
    
    
            private async Task<User_Directory> Directory_Dissasembly_Operation(string root_directory_path)
            {
    
                User_Directory current_directory = new User_Directory();
    
                current_directory.directory_name = System.IO.Path.GetFileName(root_directory_path);
    
    
    
    
    
                StringBuilder string_builder = new StringBuilder(root_directory_path);
    
    
    
                IEnumerator files_enumerator = System.IO.Directory.GetFiles(string_builder.ToString()).GetEnumerator();
    
                while(files_enumerator.MoveNext() == true)
                {
                    string_builder.Clear();
                    string_builder.Append((string)files_enumerator.Current);
    
                    System.Threading.Thread.Sleep(10);
    
                    Tuple<string, byte[]> current_item = new Tuple<string, byte[]>(System.IO.Path.GetFileName(string_builder.ToString()), await System.IO.File.ReadAllBytesAsync(string_builder.ToString()));
                    current_directory.files_list.Add(current_item);
                }
    
                files_enumerator.Reset();
    
    
    
    
    
                IEnumerator directories_enumerator = System.IO.Directory.GetDirectories(root_directory_path).GetEnumerator();
    
                while(directories_enumerator.MoveNext() == true)
                {
                    string_builder.Clear();
                    string_builder.Append((string)directories_enumerator.Current);
    
                    System.Threading.Thread.Sleep(10);
    
                    current_directory.sub_directories.Add(await Directory_Dissasembly_Operation(string_builder.ToString()));
                }
    
                directories_enumerator.Reset();
    
    
    
                string_builder.Clear();
    
                return current_directory;
            }
    
    
    

    Given that F is the number of total files and D is the total numbers of directories, it results that the time complexity for this algorithm is O(F * D) because it loops F times for the files in a directory, D times resulting in O(F * D).

    Time complexity: O(F * D)

    Space complexity: O(N)

    Explanation

    The algorithm uses two loops, to look through the files within the directory path passed as a parameter to the method and one loop that iterates through the sub-directories of the directory path passed as a parameter to the method. The file names and binary values are stored in a Tuple for each file, and these Tuples are stored in List within a class object. After all the files from the current directory path are extracted, the second loop iterates through the sub-directories of the current directory, and performs recursion by passing the path to the current sub-directory each iteration of the loop. This happens until all sub-directories are enumerated through. The method returns an instance of the User_Directory class, which will be added into a list within the User_Directory class that stores User_Directory class objects. IEnumerable objects are used in order to iterate through the arrays of files and directories due to the fact that it consumes less memory resources than traditional iteration because each element is loaded one by one, without creating unnecessary additional copies of the objects through which is iterating. The Thread.Sleep(10) methods are sleeping the thread 10 milliseconds in order to give the runtime time to manage memory resources and ease CPU usage by limiting the number of iterations for each loop to once every 10 milliseconds;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search