⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 利用opennetcf中ftp类写的一个手机利用ftp上传的例子
💻 CS
字号:
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;
using System.Net;

namespace DeviceApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NetworkCredential myCredential=new NetworkCredential("baodao","*****");
            if (UploadFile("baodao@211.95.73.93:2122", "testPath", "Sunset.jpg", myCredential))
                MessageBox.Show("seccess");
        }

        public static bool UploadFile(string host, string directory, string filePathName, ICredentials credentials) 
        {
            
            Uri uri = null;   
            
            string fileName = Path.GetFileName(filePathName);
            //if (directory == null)
            uri = new Uri(string.Format(@"ftp://{0}", host));
            //else
                //uri = new Uri(string.Format(@"ftp://{0}/{1}/", host, directory)); 
            try {          
                FtpRequestCreator creator = new FtpRequestCreator();    
                WebRequest.RegisterPrefix("ftp:", creator);   
                FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(uri); 
                //WebRequest ftpWebRequest = WebRequest.Create(uri);
                //FtpWebRequest ftpWebRequest = new FtpWebRequest(uri);
                ftpWebRequest.Credentials = credentials;
                // Getting the Request stream



                FileStream filestream = new FileStream(filePathName, FileMode.Open);
                BinaryReader fileReader = new BinaryReader(filestream); 


                Stream ftpRequestStream = ftpWebRequest.GetRequestStream();
                StreamWriter commandWriter = new StreamWriter(ftpRequestStream); 

                //StreamReader responseReader = new StreamReader(ftpRequestStream);  
  
                // Just ignore the result, but read it.    
                //String responseString = responseReader.ReadToEnd();    
       
                // Open the input file.  If the file does not exist, it's an error.         
                 
       
                // Create the reader for the local file data.          


                commandWriter.Write("TYPE I\r\n");
                commandWriter.Flush();


                String cmd = "stor " + @"\" + directory + @"\" + fileName + "\r\n";


                commandWriter.Write(cmd);
                commandWriter.Flush();	// We MUST flush before we start reading from both response and request     


                // Opening the data connection, this must be done before we issue the command.          
                Stream ftpResponseStream = ftpWebRequest.GetResponse().GetResponseStream();          
                BinaryWriter dataWriter = new BinaryWriter(ftpResponseStream);   
        
                // Prepare to send commands to the server.           
                
            
                // Set transfer type to IMAGE (binary).            
                      
     
                // Reading the request output           
                //responseReader = new StreamReader(ftpRequestStream);       
                //responseString = responseReader.ReadToEnd();             
                // Write the command to the request stream.       

                
               
                // Reading the request output            
                //responseString = responseReader.ReadToEnd();   
       
                // Allocate buffer for the data, which will be written in blocks.        
                int bufsize = 1024;           
                byte[] buf = new byte[bufsize];           
                int xcount;         
                while ((xcount = fileReader.Read(buf, 0, bufsize)) > 0) {             
                    // Send next buffer over the data connection.                  
                    dataWriter.Write(buf, 0, xcount);
                    Array.Clear(buf, 0, bufsize);
                }               
                fileReader.Close();             
                filestream.Close();            
                dataWriter.Close();         
                //responseReader.Close();           
                return true;       
            } catch (Exception ex) {            
                throw ex;         
            }       
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -