0

I have an http url from where i am able to list the directory and i have all the permissions to the HTTP directory. (Winform application)

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(http:///abc/def/);                  request.Credentials = new NetworkCredential("username","password");

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
  using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  {
    string html = reader.ReadToEnd();
    long FolderSize = response.ContentLength;
  }
}

I am getting 611 as content length. But i want to get the folder size in which the file exists. Please help.

hustlecoder
  • 187
  • 1
  • 1
  • 9
  • What does `http:///abc/def/` return? What are you getting from that url? Because if it is not designed to provide information about what you need then you have to modify that or ask the devs involved. – jegtugado Oct 09 '17 at 06:14
  • Unless the URL provides the directory listing, which you could parse, I don't see how this could work. The content-length provided is the length of the document, not the size of the directory. – ProgrammingLlama Oct 09 '17 at 06:17
  • @JohnEphraimTugado It returns all the files and folders in that url (in an html format) – hustlecoder Oct 09 '17 at 06:32
  • @hustlecoder can you show us the response you get from that url? – jegtugado Oct 09 '17 at 06:33
  • Just total up the size of all the files. – CodingYoshi Oct 09 '17 at 06:46
  • @JohnEphraimTugado I cannot provide the exact content, but it is an html format which has the links to all the folders within that url – hustlecoder Oct 09 '17 at 08:26
  • @CodingYoshi Yes i needed help with that. If you could please guide me to it – hustlecoder Oct 09 '17 at 08:27
  • See if [this](https://stackoverflow.com/questions/124492/c-sharp-httpwebrequest-command-to-get-directory-listing) helps you. – CodingYoshi Oct 09 '17 at 08:41
  • @CodingYoshi This link shows how to list the directories in the given url. But even i could get the size of one file, i could possibly sum all the files to get directory size. Please help. – hustlecoder Oct 10 '17 at 11:10
  • This is not going to be easy. Please tell me 2 things: 1. Why do you need the directory size? 2. Why over http? – CodingYoshi Oct 10 '17 at 12:20
  • @CodingYoshi I am trying to list the directories of an http url along with its size.(I am able to list the directories, facing issues with size) http url is the requirement of the project. – hustlecoder Oct 12 '17 at 05:54

2 Answers2

0

@JohnEphraimTugado I cannot provide the exact content, but it is an html format which has the links to all the folders within that url – hustlecoder

You said that it only contains links. So given the response that you are getting from http:///abc/def/ you won't get the information that you need.

If you have access to the said folder, assuming you have the path, then you can compute the file sizes per file inside that directory.

See What's the best way to calculate the size of a directory in .NET?.

protected static float CalculateFolderSize(string folder)
{
    float folderSize = 0.0f;

    try
    {
        //Checks if the path is valid or not
        if (!Directory.Exists(folder))
            return folderSize;
        else
        {
            try
            {
                foreach (string file in Directory.GetFiles(folder))
                {
                    if (File.Exists(file))
                    {
                        FileInfo finfo = new FileInfo(file);
                        folderSize += finfo.Length;
                    }
                }

                foreach (string dir in Directory.GetDirectories(folder))
                    folderSize += CalculateFolderSize(dir);
            }
            catch (NotSupportedException e)
            {
                Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
            }
        }
    }
    catch (UnauthorizedAccessException e)
    {
        Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
    }

    return folderSize;
}
jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • This will not work. The OP is asking how to get the directory size of a directory on an HTTP server. – CodingYoshi Oct 09 '17 at 08:42
  • @CodingYoshi if you actually read my answer I stated: **So given the response that you are getting from http:///abc/def/ you won't get the information that you need.** – jegtugado Oct 09 '17 at 08:47
  • Actually it is possible to do it the way the OP wants. See the comment I posted to his question and see the link. – CodingYoshi Oct 09 '17 at 08:52
0

This won't work as easily as you might think.

  1. "Folders" (aka directories) don't have a "size", they only have content.
  2. That content can, potentially, be enormous, if all sub-directories are considered.
  3. The response you are getting over HTTP in this case is intended to be red by humans, not by machines (your code).
  4. Even locally (on your HDD), calculating the "size" of a directory can take several minutes. If done over HTTP it might take many hours, even days (Step 2).

If you are only interested in the top-level (no sub-directories) content and know it to be reasonable it can be done rather easily:

Those 611 (bytes) of content length you get are the length of the HTML code the server answers with. What you need to do is to request the HTML code itself (by just performing a standard HTTP request). Once you have the HTML code you need to parse it (preferably using regex). Usually the returned HTML code not only contains a list of file-names, but also their size in bytes, once you have a regex that is able to parse those numbers, you are all set.

r41n
  • 908
  • 7
  • 18