【问题标题】:ftp upload/download in c# for windows mobile 6.5 with opencf.net.ftp在 c# 中使用 opencf.net.ftp 为 windows mobile 6.5 进行 ftp 上传/下载
【发布时间】:2015-10-28 06:42:46
【问题描述】:

我尝试进行 ftp 上传和下载,但它一直出现Nullreference 错误。可能opennetcf.net.ftp不是解决问题的最佳方法?谁能帮我解决这个问题?

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)
        {

        }

GPRS连接建立,但ftp连接后出现问题:

        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)
        {

        }

不,看起来身份验证没问题。没有问题!

我做了这个:

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

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

没关系!

但在此之后:ctacke

            try 

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

            catch (Exception ex1)

            {


                MessageBox.Show("file"+ ex1);

            }   

现在的问题是:在client.DownloadFile System.nullreferenceException 现在我不知道:(。 这可能是一些遗憾的事情......

为什么这是空的? client.DownloadFile(@"/GP_FTP/ans.txt", @"/download"); 我试过client.DownloadFile("/GP_FTP/ans.txt", "/download");client.DownloadFile("ans.txt", "/download"); 和相同的。为什么?

【问题讨论】:

  • 感谢 Gilles 的编辑!
  • 在所有这些代码中,NullReferenceException 发生在哪里?
  • client.DownloadFile(@"/GP_FTP/ans.txt", @"/download");

标签: c# ftp compact-framework windows-mobile-6.5


【解决方案1】:

这行不通:

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

如果你还没有给它赋值,你就不能对一个变量执行操作——然后你会得到一个NullReferenceExceptionnew 位在前:

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

在尝试下载文件之前指定 ftp 地址并发送用户名、密码 也应该更有意义。

【讨论】:

  • 我尝试登录另一个按钮,在连接(写入文本框)到 FTP 后尝试下载-上传,但一直到 Null reference exception。我认为问题不存在!
  • 这里可能存在多个问题 - NullReferenceException 是否仍然出现在 client.DownloadFile(@"/GP_FTP/ans.txt", @"/download");
  • 是的,我认为问题在于建立了 FTP 连接。如果这不起作用,我不知道下载文件的其他灵魂是什么。
  • @user5494221 a NullReferenceException 是应用程序中的一个简单错误,与您用于下载文件的技术无关。某处的某些变量未分配给您期望分配给的变量。
  • 谢谢“C.Evenhuis”,但我找不到在哪里! :)
猜你喜欢
  • 1970-01-01
  • 2011-09-09
  • 2015-11-02
  • 2011-08-27
  • 2011-03-18
  • 1970-01-01
  • 2012-06-25
  • 2012-05-20
  • 2012-07-29
相关资源
最近更新 更多