当前位置: 首页 > 工具软件 > Ftp Dot Net > 使用案例 >

ftpsample dot net 2.0

越心水
2023-12-01
 
  1. //Author             : Mohammed Habeeb - habeeb_matrix@hotmail.com  
  2. //Date Created       : 17th January 2007 
  3. //Description        : This class demonstartes common FTP functionalities using .net 2.0.
  4. using System;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.Net;
  13. namespace FTP24CP
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         string ftpServerIP;
  18.         string ftpUserID;
  19.         string ftpPassword;
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.             ftpServerIP = "192.168.1.21";
  27.             ftpUserID = "administrator";
  28.             ftpPassword = "xxxxx";
  29.             txtServerIP.Text = ftpServerIP;
  30.             txtUsername.Text = ftpUserID;
  31.             txtPassword.Text = ftpPassword;
  32.             this.Text += ftpServerIP;
  33.             btnFTPSave.Enabled = false;
  34.         }
  35.         /// <summary>
  36.         /// Method to upload the specified file to the specified FTP Server
  37.         /// </summary>
  38.         /// <param name="filename">file full name to be uploaded</param>
  39.         private void Upload(string filename)
  40.         {
  41.             FileInfo fileInf = new FileInfo(filename);
  42.             string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
  43.             FtpWebRequest reqFTP;
  44.             // Create FtpWebRequest object from the Uri provided
  45.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
  46.             // Provide the WebPermission Credintials
  47.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  48.             // By default KeepAlive is true, where the control connection is not closed
  49.             // after a command is executed.
  50.             reqFTP.KeepAlive = false;
  51.             // Specify the command to be executed.
  52.             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  53.             // Specify the data transfer type.
  54.             reqFTP.UseBinary = true;
  55.             // Notify the server about the size of the uploaded file
  56.             reqFTP.ContentLength = fileInf.Length;
  57.             // The buffer size is set to 2kb
  58.             int buffLength = 2048;
  59.             byte[] buff = new byte[buffLength];
  60.             int contentLen;
  61.             // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
  62.             FileStream fs = fileInf.OpenRead();
  63.            
  64.             try
  65.             {
  66.                 // Stream to which the file to be upload is written
  67.                 Stream strm = reqFTP.GetRequestStream();
  68.                 
  69.                 // Read from the file stream 2kb at a time
  70.                 contentLen = fs.Read(buff, 0, buffLength);
  71.                 // Till Stream content ends
  72.                 while (contentLen != 0)
  73.                 {
  74.                     // Write Content from the file stream to the FTP Upload Stream
  75.                     strm.Write(buff, 0, contentLen);
  76.                     contentLen = fs.Read(buff, 0, buffLength);
  77.                 }
  78.                 // Close the file stream and the Request Stream
  79.                 strm.Close();
  80.                 fs.Close();
  81.             }
  82.             catch(Exception ex)
  83.             {
  84.                 MessageBox.Show(ex.Message, "Upload Error");
  85.             }
  86.         }
  87.         public void DeleteFTP(string fileName)
  88.         {
  89.             try
  90.             {
  91.                 string uri = "ftp://" + ftpServerIP + "/" + fileName;
  92.                 FtpWebRequest reqFTP;
  93.                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
  94.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  95.                 reqFTP.KeepAlive = false;
  96.                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
  97.                 string result = String.Empty;
  98.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  99.                 long size = response.ContentLength;
  100.                 Stream datastream = response.GetResponseStream();
  101.                 StreamReader sr = new StreamReader(datastream);
  102.                 result = sr.ReadToEnd();
  103.                 sr.Close();
  104.                 datastream.Close();
  105.                 response.Close();
  106.             }
  107.             catch (Exception ex)
  108.             {
  109.                 MessageBox.Show(ex.Message, "FTP 2.0 Delete");
  110.             }
  111.         }
  112.         private string[] GetFilesDetailList()
  113.         {
  114.             string[] downloadFiles;
  115.             try
  116.             {
  117.                 StringBuilder result = new StringBuilder();
  118.                 FtpWebRequest ftp;
  119.                 ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
  120.                 ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  121.                 ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  122.                 WebResponse response = ftp.GetResponse();
  123.                 StreamReader reader = new StreamReader(response.GetResponseStream());
  124.                 string line = reader.ReadLine();
  125.                 while (line != null)
  126.                 {
  127.                     result.Append(line);
  128.                     result.Append("/n");
  129.                     line = reader.ReadLine();
  130.                 }
  131.                 
  132.                 result.Remove(result.ToString().LastIndexOf("/n"), 1);
  133.                 reader.Close();
  134.                 response.Close();
  135.                 return result.ToString().Split('/n');
  136.                 //MessageBox.Show(result.ToString().Split('/n'));
  137.             }
  138.             catch (Exception ex)
  139.             {
  140.                 System.Windows.Forms.MessageBox.Show(ex.Message);
  141.                 downloadFiles = null;
  142.                 return downloadFiles;
  143.             }
  144.         }
  145.         public string[] GetFileList()
  146.         {
  147.             string[] downloadFiles;
  148.             StringBuilder result = new StringBuilder();
  149.             FtpWebRequest reqFTP;
  150.             try
  151.             {
  152.                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
  153.                 reqFTP.UseBinary = true;
  154.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  155.                 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
  156.                 WebResponse response = reqFTP.GetResponse();
  157.                 StreamReader reader = new StreamReader(response.GetResponseStream());
  158.                 //MessageBox.Show(reader.ReadToEnd());
  159.                 string line = reader.ReadLine();
  160.                 while (line != null)
  161.                 {
  162.                     result.Append(line);
  163.                     result.Append("/n");
  164.                     line = reader.ReadLine();
  165.                 }
  166.                 result.Remove(result.ToString().LastIndexOf('/n'), 1);
  167.                 reader.Close();
  168.                 response.Close();
  169.                 //MessageBox.Show(response.StatusDescription);
  170.                 return result.ToString().Split('/n');
  171.             }
  172.             catch (Exception ex)
  173.             {
  174.                 System.Windows.Forms.MessageBox.Show(ex.Message);
  175.                 downloadFiles = null;
  176.                 return downloadFiles;
  177.             }
  178.         }
  179.         private void Download(string filePath, string fileName)
  180.         {
  181.             FtpWebRequest reqFTP;
  182.             try
  183.             {
  184.                 //filePath = <<The full path where the file is to be created.>>, 
  185.                 //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
  186.                 FileStream outputStream = new FileStream(filePath + "//" + fileName, FileMode.Create);
  187.                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
  188.                 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  189.                 reqFTP.UseBinary = true;
  190.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  191.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  192.                 Stream ftpStream = response.GetResponseStream();
  193.                 long cl = response.ContentLength;
  194.                 int bufferSize = 2048;
  195.                 int readCount;
  196.                 byte[] buffer = new byte[bufferSize];
  197.                 readCount = ftpStream.Read(buffer, 0, bufferSize);
  198.                 while (readCount > 0)
  199.                 {
  200.                     outputStream.Write(buffer, 0, readCount);
  201.                     readCount = ftpStream.Read(buffer, 0, bufferSize);
  202.                 }
  203.                 ftpStream.Close();
  204.                 outputStream.Close();
  205.                 response.Close();
  206.             }
  207.             catch (Exception ex)
  208.             {
  209.                 MessageBox.Show(ex.Message);
  210.             }
  211.         }
  212.         private void btnUpload_Click(object sender, EventArgs e)
  213.         {
  214.             OpenFileDialog opFilDlg = new OpenFileDialog();
  215.             if (opFilDlg.ShowDialog() == DialogResult.OK)
  216.             {
  217.                 Upload(opFilDlg.FileName);
  218.             }
  219.         }
  220.         private void btnDownload_Click(object sender, EventArgs e)
  221.         {
  222.             FolderBrowserDialog fldDlg = new FolderBrowserDialog();
  223.             if (txtUpload.Text.Trim().Length > 0)
  224.             {
  225.                 if (fldDlg.ShowDialog() == DialogResult.OK)
  226.                 {
  227.                     Download(fldDlg.SelectedPath, txtUpload.Text.Trim());
  228.                 }
  229.             }
  230.             else
  231.             {
  232.                 MessageBox.Show("Please enter the File name to download");
  233.             }
  234.         }
  235.         private void btnLstFiles_Click(object sender, EventArgs e)
  236.         {
  237.             string[] filenames = GetFileList();
  238.             lstFiles.Items.Clear();
  239.             foreach (string filename in filenames)
  240.             {
  241.                 lstFiles.Items.Add(filename);
  242.             }
  243.         }
  244.         private void btndelete_Click(object sender, EventArgs e)
  245.         {
  246.             OpenFileDialog fldDlg = new OpenFileDialog();
  247.             if (txtUpload.Text.Trim().Length > 0)
  248.             {
  249.                 DeleteFTP(txtUpload.Text.Trim());
  250.             }
  251.             else
  252.             {
  253.                 MessageBox.Show("Please enter the File name to delete");
  254.             }
  255.         }
  256.         private long GetFileSize(string filename)
  257.         {
  258.             FtpWebRequest reqFTP;
  259.             long fileSize = 0;
  260.             try
  261.             {
  262.                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + filename));
  263.                 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
  264.                 reqFTP.UseBinary = true;
  265.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  266.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  267.                 Stream ftpStream = response.GetResponseStream();
  268.                 fileSize = response.ContentLength;
  269.                 
  270.                 ftpStream.Close();
  271.                 response.Close();
  272.             }
  273.             catch (Exception ex)
  274.             {
  275.                 MessageBox.Show(ex.Message);
  276.             }
  277.             return fileSize;
  278.         }
  279.         private void Rename(string currentFilename, string newFilename)
  280.         {
  281.             FtpWebRequest reqFTP;
  282.             try
  283.             {
  284.                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + currentFilename));
  285.                 reqFTP.Method = WebRequestMethods.Ftp.Rename;
  286.                 reqFTP.RenameTo = newFilename;
  287.                 reqFTP.UseBinary = true;
  288.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  289.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  290.                 Stream ftpStream = response.GetResponseStream();
  291.                 
  292.                 ftpStream.Close();
  293.                 response.Close();
  294.             }
  295.             catch (Exception ex)
  296.             {
  297.                 MessageBox.Show(ex.Message);
  298.             }
  299.         }
  300.         private void MakeDir(string dirName)
  301.         {
  302.             FtpWebRequest reqFTP;
  303.             try
  304.             {
  305.                 // dirName = name of the directory to create.
  306.                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + dirName));
  307.                 reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  308.                 reqFTP.UseBinary = true;
  309.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  310.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  311.                 Stream ftpStream = response.GetResponseStream();
  312.                 ftpStream.Close();
  313.                 response.Close();
  314.             }
  315.             catch (Exception ex)
  316.             {
  317.                 MessageBox.Show(ex.Message);
  318.             }
  319.         }
  320.         private void btnFileSize_Click(object sender, EventArgs e)
  321.         {
  322.             long size = GetFileSize(txtUpload.Text.Trim());
  323.             MessageBox.Show(size.ToString()+" bytes");
  324.         }
  325.         private void button1_Click(object sender, EventArgs e)
  326.         {
  327.             Rename(txtCurrentFilename.Text.Trim(), txtNewFilename.Text.Trim());
  328.         }
  329.         private void btnewDir_Click(object sender, EventArgs e)
  330.         {
  331.             MakeDir(txtNewDir.Text.Trim());
  332.         }
  333.         private void txtPassword_TextChanged(object sender, EventArgs e)
  334.         {
  335.             btnFTPSave.Enabled = true;
  336.         }
  337.         private void txtServerIP_TextChanged(object sender, EventArgs e)
  338.         {
  339.             btnFTPSave.Enabled = true;
  340.         }
  341.         private void txtUsername_TextChanged(object sender, EventArgs e)
  342.         {
  343.             btnFTPSave.Enabled = true;
  344.         }
  345.         private void btnFTPSave_Click(object sender, EventArgs e)
  346.         {
  347.             ftpServerIP = txtServerIP.Text.Trim();
  348.             ftpUserID = txtUsername.Text.Trim();
  349.             ftpPassword = txtPassword.Text.Trim();
  350.             btnFTPSave.Enabled = false;
  351.         }
  352.         private void btnFileDetailList_Click(object sender, EventArgs e)
  353.         {
  354.             string[] filenames = GetFilesDetailList();
  355.             lstFiles.Items.Clear();
  356.             foreach (string filename in filenames)
  357.             {
  358.                 lstFiles.Items.Add(filename);
  359.             }
  360.         }
  361.         private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
  362.         {
  363.         }
  364.     }
  365. }
 类似资料:

相关阅读

相关文章

相关问答