Problems on transferring a file via ftp

Last post 07-03-2008 6:56 by Fugee47. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 07-02-2008 6:27

    Problems on transferring a file via ftp

    Hello,

    i am using the following code (sample-Program) to connect to a ftp-server from a pocket pc with win ce 4.2.

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using OpenNETCF.Net.Ftp;
    using System.IO;

    namespace FTPTest
    {
        public partial class Form1 : Form
        {
            delegate void StringDelegate(string value);

            private FTP m_ftp;

            public Form1()
            {
                InitializeComponent();
            }

            private void connect_Click(object sender, EventArgs e)
            {
                OnResponse("Connecting");
                m_ftp = new FTP(server.Text);
                m_ftp.ResponseReceived += new FTPResponseHandler(m_ftp_ResponseReceived);
                m_ftp.Connected += new FTPConnectedHandler(m_ftp_Connected);
                m_ftp.BeginConnect(user.Text, password.Text);
            }

            void m_ftp_Connected(FTP source)
            {
                // when this happens we're ready to send command
                OnResponse("Connected.");
            }

            void m_ftp_ResponseReceived(FTP source, FTPResponse Response)
            {
                OnResponse(Response.Text);
            }

            private void OnResponse(string response)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new StringDelegate(OnResponse), new object[] { response } );
                    return;
                }
                ListViewItem item = new ListViewItem(new string[] { DateTime.Now.ToShortTimeString(), response });
                status.Items.Insert(0, item);
                status.Columns[1].Width = -1;
            }

            private void getFileList_Click(object sender, EventArgs e)
            {
                FTPFiles files = m_ftp.EnumFiles();

                fileList.Items.Clear();

                foreach (FTPFile file in files)
                {
                    fileList.Items.Add( new ListViewItem( new string[] { file.Name, file.Size.ToString() } ));
                }

                tabs.SelectedIndex = 1;
            }

            private void upload_Click(object sender, EventArgs e)
            {
                FileStream stream = File.OpenRead("\\dataset.png");
                m_ftp.SendFile(stream, "dataset.png");
                stream.Close();
            }
        }
    }

     

    I get connected (according to the output of the ftp-server). But when i try to upload a file (dataset.png which exists, i have checked it with fileinfo) i get the following error:

    FTPException:   cant connect to remote server in Function "private Socket OpenDataSocket() "

     

     

     

    after that i tried it another way:

     

    private void connect()

    {

    FtpRequestCreator creator = new FtpRequestCreator();
                WebRequest.RegisterPrefix("ftp:", creator);
                FtpWebRequest reqFTP;
                //connect
                Uri testUri = new Uri("ftp://" + "10.120.60.101/");
                reqFTP = (FtpWebRequest)WebRequest.Create(testUri);
                reqFTP.Credentials = new NetworkCredential("hansolo", "gotyou");
                Stream ftpRequestStream = reqFTP.GetRequestStream();
                StreamReader reader = new StreamReader(ftpRequestStream); 

    }

     

    this way i also get connected, but i dont know how to up/download a file

     

     

    maybe someone can give me a hint for that ???

  • 07-03-2008 6:56 In reply to

    Re: Problems on transferring a file via ftp

     ok, i have found another solution:

    http://www.codeproject.com/KB/IP/ftplib.aspx

Page 1 of 1 (2 items)