Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI all,

i need to move one text file from my local system to a server.for that i did one console application.the following is the code i wrote,

C#
using System;
using System.IO;
using System.Net;
using System.Text;


namespace FileTransfer
{
    class Program
    {
        public static void Main()
        {
                       string ftppath = "ftp://149.223.26.33//D://Developers/muralim/TRW.HRM.Headcount_LMSFeed/Log/";
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftppath + @"/" + "employeedetails1.txt");//this is the text file i need to copy to the server.         

            request.Method = WebRequestMethods.Ftp.UploadFile;           
            request.Credentials = new NetworkCredential("rajeshb", "Satyam*03");
            request.Timeout = 1000000000;
            StreamReader sourceStream = new StreamReader(@"C:\Documents and Settings\aj99823\Desktop\project\employeedetails1.txt");//this is the current path of the file in the local machine        
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            request.Proxy = null;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

            response.Close();

        }
    }
}

but while executing i am getting one error saying "The underlying connection was closed: An unexpected error occurred on a receive.".i thought this error occours because of the connection timeout issue.but i gave the connection timeout period to its maximum.but still its giving the same error.now i think the problem will be regarding the the ftp URI that is " "ftp://149.223.26.33//D://Developers/muralim/TRW.HRM.Headcount_LMSFeed/Log/".i think this is not the way to give the path.can anyone plss help me on this....?????

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 23-May-12 21:02pm
v2
Comments
Sergey Alexandrovich Kryukov 24-May-12 2:51am    
Were you able to do it using some available FTP client? How do you know that an FTP server is set up properly?
--SA

1 solution

check this out.

C# FTP Client Library[^]
 
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