2015-10-28 31 views
2

我试图做一个ftp上传和下载,但它是所有的时间去Nullreference错误。可能是因为opennetcf.net.ftp不是解决问题的最佳方法吗?任何人都可以帮我解决这个问题吗?ftp上传/下载在C#为windows mobile 6.5与opencf.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) 
     { 

     } 

没有看起来的认证ok.There是没有问题的!

我做了这个:

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

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

这是OK!

但经过这样的: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.DownloadFileSystem.nullreferenceException 现在我不知道:( 它可以是有些可惜的事......

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

+0

感谢Gilles的编辑! – user5494221

+0

在所有这些代码中哪里是NullReferenceException发生? – ctacke

+0

'client.DownloadFile(@“/ GP_FTP/ans.txt”,@“/ download”);' – user5494221

回答

0

这是行不通的:

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

如果你还没有分配的东西它但你不能对一个变量进行操作 - 那么你得到一个NullReferenceException。该new位先行:

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

指定的FTP地址和之前发送的用户名,密码试图下载的文件应该能理解了。

+0

我试图登录到另一个按钮,然后连接(写入文本框)到FTP尝试下载上传但所有时间都会去'空引用异常'。我认为问题不在那里! – user5494221

+0

这里可能存在多个问题 - 'clientRecoveryException'仍然出现在'client.DownloadFile(@“/ GP_FTP/ans.txt”,@“/ download”);'? –

+0

是的,我认为问题在那里,因为FTP连接建立。如果这不起作用,我不知道下载文件的其他灵魂可能是什么。 – user5494221

相关问题