Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

i have to copy files from ftp to remote desktop computer.

C#
try
{
    File.Copy("ftp://IP Address:Port/My Folder/MyFile.dll", @"Remote IP Address\My Location");
}
catch(Exception ex)
{
    Console.Write(ex.Message);
}


But, i could not do this. I think the possible reasons might be:
(1)the ftp and remote desktop requires password (credentials).
(2)i am trying to copy file to a folder who is not shared.
(3)Any other issue........


if(1), then how can handle credentials in my code?
if(2), then how can copy files to a folder who is not shared?
if(3), tell me the actual reason and how to handle it?

thanks,
Posted

Take a look at this article on MSDN
How to: Upload Files with FTP[^].

It outlines how to use credentials and such with a very nice clean code sample.
 
Share this answer
 
v2
Comments
Espen Harlinn 4-Jan-12 11:27am    
Good link :)
Sergey Alexandrovich Kryukov 4-Jan-12 12:33pm    
Yes, but the only problem is: OP wants to download, not upload. I provided a solution, please see.
--SA
Espen Harlinn 4-Jan-12 15:56pm    
Yes, I noticed that answer nearly a year ago :)
Wonde Tadesse 4-Jan-12 17:42pm    
5+

File copy does not work with FTP. I provided a complete solution in my past answer, please see: FTP: Download Files[^].


—SA
 
Share this answer
 
Comments
Espen Harlinn 4-Jan-12 15:55pm    
5'ed!
Sergey Alexandrovich Kryukov 4-Jan-12 18:40pm    
Thank you, Espen.
--SA
Wonde Tadesse 4-Jan-12 17:42pm    
5+
Sergey Alexandrovich Kryukov 4-Jan-12 18:40pm    
Thank you, Wonde.
--SA
Harish Kumar Bansal 11-Jan-12 5:14am    
Thanks, i have successfully copy file from FTP. Now i have to Paste this file to Remote Desktop PC which does not have any shared folder. Is it possible?
You have to use FtpWebRequest & FtpWebResponse and after that you have to get response.
C#
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://path/test.htm");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","abc@xyz.com");            
            
            StreamReader sourceStream = new StreamReader("testfile.txt");
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
            response.Close();
 
Share this answer
 
v3
File.copy automatically doesn't handle FTP protocol. You need to download the file using FTP related libraries. You can use System.Net.FtpWebRequest

You can download it to a temp location or directly to the network path(UNC). Network folder need not to be shared if the process identity have admin access to the target machine. In that case the location can be accessed by \\machineName\C$\targetFolder . Change the drive letter C to whatever drive you are using.
 
Share this answer
 
v2
any one must not write code without testing because visiters have no time.so i request to all solution giver at codeproject to add only right code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900