FTP文件操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace file
{
/// <summary>
/// FTP参数类
/// </summary>
public class FtpParams
{
private static bool m_IsInit = false;
private static string m_ServerIP = "127.0.0.1";
private static int m_Port = 21;
private static string m_Dir = "/";
private static string m_User = "123";
private static string m_Password = "123";
/// <summary>
/// 获取是否初始化
/// </summary>
public static bool IsInit
{
get
{
return m_IsInit;
}
}
/// <summary>
/// 获取服务器IP
/// </summary>
public static string ServerIP
{
get
{
return m_ServerIP;
}
}
/// <summary>
/// 获取端口
/// </summary>
public static int Port
{
get
{
return m_Port;
}
}
/// <summary>
/// 获取FTP目录
/// </summary>
public static string Dir
{
get
{
return m_Dir;
}
}
/// <summary>
/// 获取用户名
/// </summary>
public static string User
{
get
{
return m_User;
}
}
/// <summary>
/// 密码
/// </summary>
public static string Password
{
get
{
return m_Password;
}
}
/// <summary>
/// 初始化FTP参数
/// </summary>
public static void Init()
{
if (m_IsInit == true)
return;
try
{
//EntityHandler tEntyHandler = EntityHandler.CreateEntityHandler(BJCommonConstString.STR_BJModelName);
//string strSQL = "from BJ_FTP_DATA_SERVER ";
//IList tTaskLst = tEntyHandler.GetEntities(strSQL);
//if (tTaskLst.Count == 0)
// return;
//BJ_FTP_DATA_SERVER tDataservice = null;
//tDataservice = tTaskLst[0] as BJ_FTP_DATA_SERVER;
//m_ServerIP = tDataservice.SERVERIP;
//m_Port = Convert.ToInt32(tDataservice.SERVERPORT);
//m_Dir = tDataservice.SERVERDIRECTORY;
//m_User = tDataservice.USERNAME;
//m_Password = tDataservice.PASSWORD.ToString();
//m_IsInit = true;
}
catch (Exception ex)
{
throw new Exception("FTP参数初始化失败:" + ex.Message);
}
}
}
}
///界面操作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.COM.SDM.Ftp;
using System.IO;
namespace file
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ListViewItem tItem = null;
OpenFileDialog tOpenFileDlg = new OpenFileDialog();
tOpenFileDlg.Title = "请选择要上传的文件";
tOpenFileDlg.Filter = "所有文件(*.*)|*.*";
if (tOpenFileDlg.ShowDialog() == DialogResult.OK)
{
bool bl = false;
tItem = new ListViewItem();
tItem.BackColor = System.Drawing.Color.Gray;
tItem.Text = tOpenFileDlg.FileName;
foreach (ListViewItem item in this.lswFileList.Items)
{
if (tItem.Text == item.Text)
{
bl = true;
break;
}
}
if (bl == false)
{
this.lswFileList.Items.Add(tItem);
}
}
}
catch (Exception ex)
{
MessageBox.Show("" + ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
int tFilecount = 0;
if (this.lswFileList.Items.Count == 0)
{
MessageBox.Show("请选择要上传的文件", "信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//连接Ftp服务器
FTP tFtp = new FTP();
tFtp.UploadCompleteEv += new AsyncCompletedEventHandler(ftp_UploadComplete);
tFtp.UploadProgressEv += new ProgressChangedEventHandler(ftp_UploadProgress);
tFtp.Connect(FtpParams.ServerIP, FtpParams.User, FtpParams.Password);
//在服务器上建立文件夹
string tUpFileDir = "\\" + "7";
tFtp.MakeDirectory(tUpFileDir);
tFtp.ChangeDirectory(tUpFileDir);
foreach (ListViewItem tViewItem in this.lswFileList.Items)
{
string txtSendFileName = tViewItem.Text;
string strFileName = Path.GetFileName(txtSendFileName.Trim());
//删除已有同名文件:存在隐患,是否应该设置提示信息?
if (File.Exists("D:\\123"+tUpFileDir+"\\"+strFileName) == true)
{
tFtp.RemoveFile(strFileName);
tFtp.Upload(strFileName, txtSendFileName, true);
}
else
{
tFtp.Upload(strFileName, txtSendFileName, true);
}
tFilecount++;
bool tbl = tFtp.UploadComplete(strFileName);
if (tbl == false)
{
MessageBox.Show("文件:" + txtSendFileName + "未上传成功!");
}
}
}
catch (Exception ex)
{
MessageBox.Show("上传失败!具体原因:" + ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (MessageBox.Show("文件上传成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
{
this.Close();
}
}
/// <summary>
/// 创建上传文件根目录
/// </summary>
/// <param name="tFtpClient">ftp服务</param>
/// <param name="tUpFileDir">上传目录</param>
//private void CreateFileDir(FTP tFtpClient, string tUpFileDir)
//{
// string strForlder = "";
// //string[] strArray = tUpFileDir.Split('/');
// //foreach (string str in strArray)
// //{
// strForlder = strForlder + tUpFileDir;
// tFtpClient.MakeDirectory(strForlder);
// //strForlder = strForlder + "//";
// //}
//}
/// <summary>
/// 上传文件进度条显示
/// </summary>
private void ftp_UploadProgress(object sender, ProgressChangedEventArgs e)
{
this.Cursor = Cursors.WaitCursor;
this.pgbProgress.Visible = true;
this.pgbProgress.Maximum = 100;
this.pgbProgress.Value = e.ProgressPercentage;
FtpFileInfo tFileInfo = e.UserState as FtpFileInfo;
long fileSize = tFileInfo.FileSize;
long downSize = fileSize * e.ProgressPercentage / 100;
this.lblState.Text = "已上传" + downSize.ToString() + "字节,总共" + fileSize.ToString() + "字节";
Application.DoEvents();
}
/// <summary>
/// 上传完成提示
/// </summary>
private void ftp_UploadComplete(object sender, AsyncCompletedEventArgs e)
{
FtpFileInfo tFileInfo = e.UserState as FtpFileInfo;
string tFileName = Path.GetFileName(tFileInfo.LocalFileName);
this.Cursor = Cursors.Default;
this.pgbProgress.Visible = false;
this.lblState.Text = tFileName + "文件上传完成";
Application.DoEvents();
}
}
}


浙公网安备 33010602011771号