实例介绍
【实例简介】
【实例截图】
【核心代码】
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("更改目录成功!");
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论