Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i am trying to upload file on my php server by writing program in C# console
it read the file for every 5 minutes and upload the file on the server
but my code is giving me error "unable to connect to server"

please help me to set the paths right for local file and online server .





static void Main(string[] args)
        {
            StreamReader streamReader = new StreamReader("C:\\wamp\\www\\FTCPC\\logfile.txt");
            ArrayList lines = new ArrayList();

            string line;

            while ((line = streamReader.ReadLine()) != null)
            {
                lines.Add(line);
            }
            streamReader.Close();

            if (lines.Count > 0)
            {
                Console.Write(lines[lines.Count - 1].ToString());
               
            }

            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("username", "password", "server info ");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader("C:\\wamp\\www\\FTCPC\\logfile.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();

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

            response.Close();



            Console.WriteLine("\\n");
            Console.ReadLine();

        }
Posted

1 solution

C#
request.Credentials = new NetworkCredential("username", "password", "server info ");


I assume you changed this so we could not see your username and password ? If so, then an error that your code cannot connect is best 'debugged' by making sure you can connect to your FTP site in general on that machine, checking your firewall is letting your program through, etc.
 
Share this answer
 
Comments
vicky87 12-Aug-11 16:44pm    
yes i have change the username/password so no one can see :) and i have add my program into my windows firewall but still same error.
vicky87 12-Aug-11 17:18pm    
i have mange to upload the file but can any one tell me how i can upload file after every 5 minutes ..by using any loop or some using some time function
Christian Graus 12-Aug-11 17:52pm    
If all you know how to do is copy code randomly from the web, and you don't know how to use google, or write simple code like a timer, perhaps you need to buy a beginners book ? You need to set a timer, that is all.
vicky87 15-Aug-11 4:01am    
@christian the this is that i am PHP developer and my bro need to make few changes in C# program and i have not worked on C# programing much ..and you are right need some beginners books for this :)..

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