0

I am developing a software for downloading a website in C# but I have some trouble in copying the folder from server to the local directory. I am implementing following code for this purpose;

  public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
  {
      try
      {
          foreach (DirectoryInfo dir in source.GetDirectories())
                CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
          foreach (FileInfo file in source.GetFiles())
                file.CopyTo(Path.Combine(target.FullName, file.Name));
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message, "Form2", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
  }    

And the function call is

private void button4_Click(object sender, EventArgs e)
{                
    try
    {
         CopyFilesRecursively(new DirectoryInfo(@"https:facebook.com"), new DirectoryInfo(@"G:\Projects\"));
    }
    catch (Exception ex)
    {
         MessageBox.Show(ex.Message, "Form2", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}   

The message box shows that "the given path format is not supported."

Irshad
  • 3,071
  • 5
  • 30
  • 51
  • `DirectoryInfo` is for a local or network path, not for an https resource. – Rob Mar 15 '16 at 03:24
  • Maybe you need a authentication to see an remote path, look at http://stackoverflow.com/questions/5433570/access-a-remote-directory-from-c-sharp – Alisom Martins Mar 15 '16 at 03:26
  • Rob! what for downloading folder from https resource? – Sardar Ajmal Khan Mar 15 '16 at 03:39
  • Possible duplicate of [How to download a file from a URL in C#?](http://stackoverflow.com/questions/307688/how-to-download-a-file-from-a-url-in-c) – Rob Mar 15 '16 at 03:40
  • Are you want to download a file or a complete web site? – Irshad Mar 15 '16 at 03:43
  • Rob..this is for downloading a file.....i need help for downloading complete website with its links...a complete website may have different webpages with different extensions...i cannot give a specific extension in code – Sardar Ajmal Khan Mar 15 '16 at 03:51

1 Answers1

0

As we are aware, all the web sites hosted on internet uses virtual path's (which are more readable and provide more security) for their files and folders. Actual files and folders are on a server behind those virtual path. So to copy a file or a folder from a remote server we need the actual path of the resource.

I provide following code snippet for downloading a file from a server which deployed by my self (I know the directory structure of it of course)

string filename = "MyPage.xml";
string filesource = Server.MapPath("~/MyFiles/") + filename; // server file "MyPage.xml" available in server directory "files"
System.IO.FileInfo fi = new System.IO.FileInfo(filesource);
string filedest = System.IO.Path.GetTempPath()+"MyFile.xml";
fi.CopyTo(filedest);

These are some other SO posts you can look for;

How to download a file from a URL in C#?
Copy image file from web url to local folder?
how to copy contents of a website using a .net desktop application
how to copy all text from a certain webpage and save it to notepad C#
How do you archive an entire website for offline viewing?

Community
  • 1
  • 1
Irshad
  • 3,071
  • 5
  • 30
  • 51