基本信息
源码名称:C# ftp文件上传 例子源码
源码大小:0.23M
文件格式:.rar
开发语言:C#
更新时间:2014-11-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication4.Properties;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
string filePath;
string fileName;
string ftpServerIP;
string ftpServerPort;
string ftpUserID;
string ftpPassword;
private Settings settings=new Settings();
FolderBrowserDialog folderBrowserDialog =new FolderBrowserDialog();
private ContextMenu notifyiconMnu;
bool isConnected=false;
Timer time = new Timer();
public Form1()
{
InitializeComponent();
textBox1.Text = this.settings.ServerIp;
textBox2.Text = this.settings.ServerPort;
textBox3.Text = this.settings.UserName;
textBox4.Text = this.settings.Password;
ftpServerIP = textBox1.Text;
ftpServerPort = textBox2.Text;
ftpUserID = textBox3.Text;
ftpPassword = textBox4.Text;
filePath = this.settings.SourceDirectory;
time.Interval = 1000;
time.Tick = new EventHandler(time_tick);
notifyIcon1.Visible = false;
this.Paint =Form1_Paint;
this.SizeChanged =Form1_SizeChanged;
}
public void Form1_Paint(object sender, EventArgs e)
{}
private void Initializenotifyicon()
{
//定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象
MenuItem[] mnuItms = new MenuItem[3];
mnuItms[0] = new MenuItem();
mnuItms[0].Text = "显示窗口";
mnuItms[0].Click = new System.EventHandler(this.notifyIcon1_showfrom);
mnuItms[1] = new MenuItem("-");
mnuItms[2] = new MenuItem();
mnuItms[2].Text = "退出系统";
mnuItms[2].Click = new System.EventHandler(this.ExitSelect);
mnuItms[2].DefaultItem = true;
notifyiconMnu = new ContextMenu(mnuItms);
notifyIcon1.ContextMenu = notifyiconMnu;
notifyIcon1.DoubleClick =notifyIcon1_DoubleClick;
//为托盘程序加入设定好的ContextMenu对象
}
public void notifyIcon1_DoubleClick(object sender,EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
public void notifyIcon1_showfrom(object sender, System.EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
public void ExitSelect(object sender, System.EventArgs e)
{
//隐藏托盘程序中的图标
notifyIcon1.Visible = false;
//关闭系统
this.Close();
this.Dispose(true);
}
public void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized) //判断是否最小化
{
this.ShowInTaskbar = false; //不显示在系统任务栏
notifyIcon1.Visible = true; //托盘图标可见
Initializenotifyicon();
}
}
public void time_tick(object sender ,EventArgs e)
{
int i;
int l = dataGridView1.Rows.Count;
DirectoryInfo theFolder = new DirectoryInfo(filePath);
FileInfo[] fileInfo = theFolder.GetFiles();
i = fileInfo.Length;
if (i != 0)
{
for (int j = i - 1; j >= 0; j--)
{
fileName = fileInfo[j].Name;
if(fileName!=null)
{
label6.Text = fileName;
UploadFtp(filePath, fileName, ftpServerIP, ftpServerPort, ftpUserID, ftpPassword);
File.Delete(filePath "\\" fileName);
string[] row = {"成功",fileName,DateTime.Now.ToString() };
dataGridView1.Rows.Add(row);
// dataGridView1.Rows[l - 1].Cells[0].Value = Image.FromFile("E:/VsProject/WindowsFormsApplication2/WindowsFormsApplication4/1.png");
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
// 要上传的文件
/* ServiceReference1.WebService4SoapClient pp = new ServiceReference1.WebService4SoapClient();
FileStream file = new FileStream("F:/BaiduYunDownload/Screen at 2014-10-12 09.37.57UTC.tif", FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[file.Length];
//读取文件保存到字节流对象
file.Read(bytes, 0, bytes.Length);
string userID="admin";
string fileNewName = "Screen at 2014-10-12 09.37.57UTC.tif";
// IFtmBasFiles ftmBasfiles = FtmBasFilesFactory.Owner.Create();
pp.UploadFilesAsync(userID, fileNewName, bytes);*/
this.settings.ServerIp = textBox1.Text;
this.settings.ServerPort = textBox2.Text;
this.settings.UserName = textBox3.Text;
this.settings.Password = textBox4.Text;
this.settings.Save();
ftpServerIP = textBox1.Text;
ftpServerPort = textBox2.Text;
ftpUserID = textBox3.Text;
ftpPassword = textBox4.Text;
isConnected = connectFtp(filePath, fileName, ftpServerIP, ftpUserID, ftpPassword);
if (isConnected)
{
time.Start();
MessageBox.Show("连接成功!");
}
else
{
time.Stop();
MessageBox.Show("连接失败,请修改信息!");
}
}
public bool connectFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword)
{
Socket socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ftpServerIP),4383);
// 链接
try
{
socketControl.Connect(ep);
return true;
}
catch (Exception ex)
{
//throw new IOException("Couldn't connect to remote server");
return false;
}
}
public int UploadFtp(string filePath, string filename, string ftpServerIP,string port, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(filePath "\\" filename);
string uri = "ftp://" ftpServerIP "/" fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" ftpServerIP ":" port "/" fileInf.Name));
try
{
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength =10240;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
//FileStream fs = fileInf.OpenRead();
FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
int n=0;
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
int pvalue = (n 10240)*100 / (int)fileInf.Length;
progressBar1.Value = pvalue ;
n = n 10240;
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
return 0;
}
catch (Exception ex)
{
reqFTP.Abort();
// Logging.WriteError(ex.Message ex.StackTrace);
return -2;
}
}
private void button2_Click(object sender, EventArgs e)
{
this.folderBrowserDialog.Description = "文件上传源目录";
if (this.folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
{
filePath = this.folderBrowserDialog.SelectedPath;
this.settings.SourceDirectory=filePath;
this.settings.Save();
}
MessageBox.Show("更改目录成功!");
}
}
}