C#WinForm实现对前一篇文章中的SFTP工具类的使用
1.winform界面设计图
2.Form1的构造方法,设置CheckForIllegalCrossThreadCalls可以忽略跨线程造成的冲突
public Form1()
{
InitializeComponent();
InitExcuteTimer();
CheckForIllegalCrossThreadCalls = false;
}
3.初始化定时器
private void InitExcuteTimer()
{
for (int i = 0; i < 60; i++)
{
cmbMin.Items.Add(i.ToString().PadLeft(2, '0'));
cmbSec.Items.Add(i.ToString().PadLeft(2, '0'));
}
}
4.设置定时任务,间隔时间可自行改动代码进行设置
private void TimerPlan(object sender, System.Timers.ElapsedEventArgs e)
{
var intMinute = e.SignalTime.Minute;
var intSecond = e.SignalTime.Second;
var intHour = e.SignalTime.Hour;
//每天8的倍数小时数信息框清空
if (intHour % 8 == 0 && intMinute == Convert.ToInt32(cmbMin.Text) &&
intSecond == Convert.ToInt32(cmbSec.Text))
{
msgTB.Text = "";
}
//每小时连一次sftp获取文件
if (intMinute == Convert.ToInt32(cmbMin.Text) && intSecond == Convert.ToInt32(cmbSec.Text))
{
msgTB.Text += DateTime.Now.ToShortDateString() + ":开始连接sftp!\r\n";
GetPicklistFile();
}
}
5.设置按钮的点击事件
private void stBT_Click(object sender, EventArgs e)
{
System.Timers.Timer timer = new System.Timers.Timer();
if (stBT.Text.Contains("开始"))
{
stBT.Text = "停止";
cmbMin.Enabled = false;
cmbSec.Enabled = false;
timer.Interval = 1000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerPlan);
//timer.AutoReset = true;
timer.Start();
}
else
{
stBT.Text = "开始";
cmbMin.Enabled = true;
cmbSec.Enabled = true;
timer.Stop();
timer.Close();
}
}
5.获取SFTP特定路径下的文件
private void GetPicklistFile()
{
string filepath = @"XXX";
string local = Application.StartupPath + "\\XXX";
string info = "";
string error = "";
try
{
SFTPHelper sftpHelper = new SFTPHelper("XXX", "XXX", "XXX", "XXX");
info=sftpHelper.Connect();
if (sftpHelper.Connected)
{
msgTB.Text += DateTime.Now.ToString() + ":" + info + "\r\n";
WriteLogs("info",info);
var files = sftpHelper.GetFileList(filepath, ".zip");//设置获取文件的格式
if (files.Count == 0)
{
msgTB.Text += DateTime.Now.ToString() + ":路径" + filepath + "下没有zip文件\r\n";
WriteLogs("error", ":路径" + filepath + "下没有zip文件");
}
else
{
foreach (var file in files)
{
var dl = sftpHelper.Get(filepath + "/" + file.ToString(), local + "\\" + file.ToString());
if (dl.Contains("成功"))
{
info = dl;
msgTB.Text += DateTime.Now.ToString() + ":" + file.ToString() + "下载成功\r\n";
WriteLogs("info", info);
var del = sftpHelper.Delete(filepath + "/" + file.ToString());
if (del.Contains("成功"))
{
info = del;
msgTB.Text += DateTime.Now.ToString() + ":" + file.ToString() + "删除成功\r\n";
WriteLogs("info", info);
}
else
{
error = del;
msgTB.Text += DateTime.Now.ToString() + ":" + file.ToString() + "删除失败\r\n";
WriteLogs("error", error);
}
}
else
{
error = dl;
msgTB.Text += DateTime.Now.ToString() + ":" + file.ToString() + "下载失败\r\n";
WriteLogs("error",error);
}
}
}
}
info = sftpHelper.Disconnect();
msgTB.Text += DateTime.Now.ToString() + ":与SFTP断开连接\r\n";
WriteLogs("info", info);
}
catch (Exception ex)
{
error = ex.Message;
msgTB.Text += DateTime.Now.ToString() + ":" + ex.ToString() + "\r\n";
WriteLogs("error", error);
}
}
6.简单地记录一下各个操作产生的日志
public static void WriteLogs(string type, string content)
{
string path = Application.StartupPath;
if (!string.IsNullOrEmpty(path))
{
path = path + "\\" + type;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
if (!File.Exists(path))
{
FileStream fs = File.Create(path);
fs.Close();
}
if (File.Exists(path))
{
StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + type + "-->" + content);
sw.WriteLine("----------------------------------------");
sw.Close();
}
}
}