2

I tried to make an ftp upload and download but it is going all time to Nullreference error. It can be that the opennetcf.net.ftp is not the best way to solve the problem? Can anyone help me to solve the problem?

namespace ftp_load
{



    public partial class Form1 : Form
    {

        public class FTPManagerClass
        {
            private static string password = "";
            private static string username = "";
            private static string host = "";

            private FtpWebRequest ftpRequest = null;
            private FtpWebResponse ftpResponse = null;
            private Stream ftpStream = null;
            // private int bufferSize = 2048;

            public FTPManagerClass(string user, string pass, string hostname)
            {
                username = user;
                password = pass;
                host = hostname;

            }

            public void DownloadFile(string remoteFile, string localFIle)
            {
                try
                {
                    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                    ftpRequest.Credentials = new NetworkCredential(username, password);
                    //ftpRequest.UseBinary = true;
                    ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                    ftpStream = ftpResponse.GetResponseStream();
                    FileStream fs = new FileStream(localFIle, FileMode.OpenOrCreate);

                    fs.Close();
                    ftpStream.Close();
                    ftpResponse.Close();
                    ftpRequest = null;


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }

            public void UploadFile(string localFile, string remoteFile)
            {
                try
                {
                    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                    ftpRequest.Credentials = new NetworkCredential(username, password);
                    //  ftpRequest.UseBinary = true;
                    // ftpRequest.UsePassive = true;
                    ftpRequest.KeepAlive = false;
                    ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                    ftpStream = ftpRequest.GetRequestStream();
                    FileStream lfs = new FileStream(localFile, FileMode.Open);
                    byte[] bytebuffer = new byte[lfs.Length];
                    int bytesSend = lfs.Read(bytebuffer, 0, (int)lfs.Length);
                    try
                    {

                        while (bytesSend != -1)
                        {
                            ftpStream.Write(bytebuffer, 0, bytesSend);
                            bytesSend = lfs.Read(bytebuffer, 0, (int)lfs.Length);

                        }
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.Message);
                    }
                    ftpResponse.Close();
                    ftpStream.Close();
                    lfs.Close();
                    ftpRequest = null;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }

        FTPManagerClass client;
        private static string password = "";
        private static string username = "";
        private static string host = "";

        private FtpWebRequest ftpRequest = null;
        private FtpWebResponse ftpResponse = null;
        private Stream ftpStream = null;


        public Form1()
        {
            InitializeComponent();
            connMgr = new ConnMgr();
            connMgr.StatusChanged += new ConnMgr.StatusChangedEventHandler(StatusChanged_Handler);
        }
        private ConnMgr connMgr;
        private void button1_Click(object sender, EventArgs e)
        {
            if (txt_br_down.Text != "")
            {
                client.DownloadFile(@"/GP_FTP/ans.txt", @"/download");
                client = new FTPManagerClass("usr", "pwd", "ftp://ftp.tzf.com");
            }

            else
            {
                MessageBox.Show("Ures mezo");
            }
        }






        private void txt_br_dw_1_TextChanged(object sender, EventArgs e)
        {

        }

The GPRS connection builds up, but after the ftp connection has some problem:

        private void btn_login_Click(object sender, EventArgs e)
        {

        }

        private void btn_login_Click_1(object sender, EventArgs e)
        {
            if (!connMgr.Connected)
            {
                connMgr.Connect("pannon", ConnMgr.ConnectionMode.Synchronous, " ", " ", "net");
                txtLog.Text += "Sync connection successful\r\n";
            }
            else
            {
                MessageBox.Show("No connection!");
            }
  }

        private void txtLog_TextChanged(object sender, EventArgs e)
        {

        }
        private void StatusChanged_Handler(object sender, ConnMgr.StatusChangedEventArgs e)
        {
            connMgr.Timeout = 3000;
            txtLog.Text += e.desc + "\r\n";         // Show current state's description

            if (!connMgr.Waiting)
                if (connMgr.Connected)
                    txtLog.Text += "Confirmed - We are now connected\r\n";
                else
                {
                    txtLog.Text += "Confirmed - Connection instance has been released\r\n";
                    // btnCancel.Enabled = false;
                    // btnConnect.Enabled = true;
                }

            if (e.connstatus == ConnMgr.ConnectionStatus.ExclusiveConflict)
                MessageBox.Show("If using Activesync, check connection settings for 'Allow wireless connection ...' or remove from homebase.");
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

No it looks the authentication is ok.There is no problem!

I made this:

  try
                {
                  client = new FTPManagerClass("usr", "pwd", "ftp://ftp.tzf.com");
                }
                catch (Exception ex)
                {

                    MessageBox.Show("Login"+ ex);
                }

This is ok!

But after this: ctacke

            try 

            {
                MessageBox.Show("before download");
               client.DownloadFile("/GP_FTP/ans.txt", "/download");
               MessageBox.Show("after download");
            }

            catch (Exception ex1)

            {


                MessageBox.Show("file"+ ex1);

            }   

The problem is now : In client.DownloadFile System.nullreferenceException Now i have no idea :(. it can be some pity thing...

Why this is null? client.DownloadFile(@"/GP_FTP/ans.txt", @"/download"); i tried client.DownloadFile("/GP_FTP/ans.txt", "/download"); and client.DownloadFile("ans.txt", "/download"); and the same. WHY?

1 Answers1

0

This won't work:

client.DownloadFile(@"/GP_FTP/ans.txt", @"/download");
client = new FTPManagerClass("usr", "pwd", "ftp://ftp.tzf.com");

You can't perform operations on a variable if you have not assigned something to it yet - then you get a NullReferenceException. The new bit goes first:

client = new FTPManagerClass("usr", "pwd", "ftp://ftp.tzf.com");
client.DownloadFile(@"/GP_FTP/ans.txt", @"/download");

Specifying the ftp address and sending the username, password before trying to download the file should make more sense, too.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • I tried to take a login to another button and after to connect( write to textbox ) to FTP tried to download-upload but all time going to `Null reference exception`. I think the problem is not there! – user5494221 Nov 04 '15 at 13:03
  • There could be multiple problems here - does the `NullReferenceException` still occur at `client.DownloadFile(@"/GP_FTP/ans.txt", @"/download");`? – C.Evenhuis Nov 04 '15 at 13:31
  • Yes i think the problem is there because the FTP connection builds up. I don't know what can be the other soultion for download file if this not works. – user5494221 Nov 04 '15 at 14:12
  • @user5494221 a `NullReferenceException` is a simple error in an application, which has nothing to do with the technology you use for downloading a file. Some variable somewhere is not assigned to what you expect it to be assigned to. – C.Evenhuis Nov 04 '15 at 14:53
  • Thx ´C.Evenhuis´ but i could not find where! :) – user5494221 Nov 05 '15 at 14:23