-3

I am trying to copy my files from main domain to sub-domain folded

i used to the code as below

CopyFiles("http://192.168.10.119:8010/1.jpg", "http://192.168.10.120:8010/TestFolder/1.jpg");

 public void CopyFiles(string sourcePath, string destinationPath)
    {
        string[] files = System.IO.Directory.GetFiles(sourcePath);
        Parallel.ForEach(files, file =>
        {
            System.IO.File.Copy(file, System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(file)));

        });
    }

then i got a error :

URI formats are not supported.

Can any one give a solution to this issue. Thanks :)

Gitz
  • 810
  • 1
  • 17
  • 48
  • 4
    `File.Copy` can only do copying on the local file system. If you want to do something like this over HTTP, perhaps look into a HTTP extension protocall called [WebDAV](http://en.wikipedia.org/wiki/WebDAV) for a solution. IIS supports it (AFAIK), but it would have to be enabled on at least the target host. – stakx - no longer contributing Sep 18 '14 at 07:10
  • `File.Copy` can also copy using network shares, so if you can set that up, you can still use `File.Copy` to do the copying, e.g. `\\192.168.10.119\WebShare\1.jpg` to `\\192.168.10.120\WebShare\TestFolder\1.jpg`. – Luaan Sep 18 '14 at 07:14
  • Where will this code run?? On one of the two servers? or someother remote admin machine? or something else?? In short, please describe the scenario in more detail (more specific is better, what problem are you trying to solve from users perspective.. who is the user? a server admin).. otherwise you'll get 100s of solutions suggested to you.. many of which will likely be far from being suited to your *needs*.. like - WebDAV.. Do you even have access to the IIS server? Are you even allowed to install components if not already present, or are you allowed to run FTP service? – Vikas Gupta Sep 18 '14 at 07:19
  • but my requirement is to do this and don't know why people down voted my question. – Gitz Sep 18 '14 at 07:20
  • i know you all are TOPPERS. that's why i share my questions there with hope the best way but all are trying to down the morale – Gitz Sep 18 '14 at 07:25
  • What is a TOPPER? Anyway, the solution is to not use URI formats, since they are not supported. –  Sep 24 '14 at 07:52

3 Answers3

2

System.IO.File.Copy cannot move files from one URL (over http) to another. You need to specify valid, local, paths such as

CopyFiles("C:\\BasePath\\1.jpg", "C:\\Target\\TestFolder\\1.jpg");

This means you need to run the program locally on the computer with the files or can access them though the local network (without going over the internet).

If you want to download an image from a remote webserver url you can find information on that here copy image from url

The writing to a remote location gets trickier, you basically need to have an FTP server on the remote host (the one receiving the file) to send files to, how to send files to FTP. Webservers don't just accept any file you send to them, Imagine if anyone could just upload a file to any server they wanted

Community
  • 1
  • 1
JensB
  • 6,663
  • 2
  • 55
  • 94
  • is any other way to copy files from domain directory to sub-domain directory ? – Gitz Sep 18 '14 at 07:07
  • Reading a file from a webserver is easy, writing to an FTP is a bit harder as if this was easy it would pose a security risk. – JensB Sep 18 '14 at 07:14
0

you no need to provide path like "192.168.10.119:8010/1.jpg" as, server can directly access folder withing same server, So you can provide path like /YourFolderName/fileName.jpg"

Hope this will help you.

      CopyFiles("/1.jpg", "/TestFolder");



 public void CopyFiles(string sourcePath, string destinationPath)
    {
            string filePath = System.Web.HttpContext.Current.Server.MapPath(destinationPath).ToString();
            sourcePath = System.Web.HttpContext.Current.Server.MapPath(sourcePath);
            System.IO.File.Copy(sourcePath, System.IO.Path.Combine(filePath, System.IO.Path.GetFileName(sourcePath)));
    }
JJRS
  • 47
  • 5
-2

Please make following changes to your code. Hope this will help you, This code will going to copy all images from one place to another.

Function Call

 CopyFiles("/DomainFolder/Images", "/DomainFolder/SubDomainFolder/Images");

Method

public void CopyFiles(string sourcePath, string destinationPath)
    {
        string[] files = System.IO.Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath(sourcePath));
        foreach (var file in files)
        {
            string filePath = System.Web.HttpContext.Current.Server.MapPath(destinationPath).ToString();

            System.IO.File.Copy(file, System.IO.Path.Combine(filePath, System.IO.Path.GetFileName(file)));

        };
    }

Thanking You,

Regards, JJRS

JJRS
  • 47
  • 5
  • getting the error '192.168.10.119:8010/1.jpg' is not a valid virtual path. – Gitz Sep 18 '14 at 07:34
  • please don't provide image path like "192.168.10.119:8010/1.jpg", you need to provide valid path for your domain, where exactly your image is stored. "FolderName/ImageName.jpg" – JJRS Sep 18 '14 at 08:43
  • but my file stores in this path which i have written :( – Gitz Sep 18 '14 at 09:09
  • Please copy paste following code to your solution. CopyFiles("/1.png", "/TestFolder"); public void CopyFiles(string sourcePath, string destinationPath) { string filePath = System.Web.HttpContext.Current.Server.MapPath(destinationPath).ToString(); sourcePath = System.Web.HttpContext.Current.Server.MapPath(sourcePath); System.IO.File.Copy(sourcePath, System.IO.Path.Combine(filePath, System.IO.Path.GetFileName(sourcePath))); } – JJRS Sep 18 '14 at 09:24