📄 ftpform.cs
字号:
private void btUpload_Click(object sender, EventArgs e)
{
if (!File.Exists(lbfilename.Text))
{
MessageBox.Show("文件不存在!");
return;
}
if (string.IsNullOrEmpty(txtFTPPath.Text))
{
MessageBox.Show("请输入上传地址!");
return;
}
UpLoadRun();
}
private void OnProgressEvent(object sender, long mini, long max, long value)
{
try
{
this.progressBar1.Maximum = (int)max;
this.progressBar1.Minimum = (int)0;
this.progressBar1.Value = (int)value;
///每处理20兆则分派消息
if (value % 20480 == 0)
Application.DoEvents();
}
catch { ;}
}
/// <summary>
/// 最下
/// </summary>
private void RefLog()
{
txtLog.SelectionStart = txtLog.Text.Length;
txtLog.SelectionLength = 0;
txtLog.ScrollToCaret();
}
static string TestResult = "";
static long ExexcuteSeepStartTime = 0;
private void UpLoadRun()
{
ErrMessage = null;
string isoFileName = lbfilename.Text.Substring(lbfilename.Text.LastIndexOf('\\') + 1);
try
{
TestResult = "";
ExexcuteSeepStartTime = 0;
String PathName = "";
ExexcuteSeepStartTime = DateTime.Now.Ticks;
txtLog.Text += "开始上传文件.." + "\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
toolStripStatusLabel1.Text = "上传中";
RefLog();
//事件开始
if (txtFTPPath.Text[0].ToString() == @"\")//拷贝
{
StreamCopy scc = new StreamCopy();
scc.OnProgress += this.OnProgressEvent;
if (File.Exists(lbfilename.Text))
{
PathName = lbfilename.Text;
}
else
{
if (ISOCreater.WorkState==true)
{
MessageBox.Show("未找到文件,请重新选择!");
}
return;
}
string UncPath = this.txtFTPPath.Text;
if (UncPath[UncPath.Length - 1].ToString() != @"\")
{
UncPath += @"\";
}
Filesize = scc.CopyFile(PathName, UncPath + isoFileName, 1024).ToString();
//事件结束
TestResult = TimeSpan.FromTicks(DateTime.Now.Ticks - ExexcuteSeepStartTime).ToString();
tb.Text += "文件上传完毕!" + "\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n耗费时间:" + TestResult + "\r\n";
ISOCreater.WorkState = false;
pg.Value = 0;
tssl.Text = "等待处理";
Console.WriteLine("Over Make!");
}
else//FTP上传
{
if (File.Exists(lbfilename.Text))
{
PathName = lbfilename.Text;
FileStream fs = new FileStream(lbfilename.Text, FileMode.Open, FileAccess.Read);
Filesize=fs.Length.ToString();
fs.Close();
}
else
{
if (ISOCreater.WorkState==true)
{
MessageBox.Show("未找到文件,请重新选择!");
}
return;
}
string path = PathName.Replace(PathName.Substring(PathName.LastIndexOf("\\")), "") + "\\";
string name = PathName.Substring(PathName.LastIndexOf("\\")).Replace("\\", "");
ISOCreater.WorkState = true;
if (txtFTPPath.Text[txtFTPPath.Text.Length - 1].ToString() != @"/")
{
txtFTPPath.Text += @"/";
}
UploadFileInBackground2(txtFTPPath.Text + name, PathName);
RefLog();
}
//事件结束
}
catch(Exception exc)
{
// ErrMessage = exc.Message;
MessageBox.Show(exc.Message);
toolStripStatusLabel1.Text = "等待处理";
txtLog.Text += "操作失败:"+exc.Message;
return;
}
}
private void btexit_Click(object sender, EventArgs e)
{
if (progressBar1.Value >= progressBar1.Minimum && ISOCreater.WorkState == true)
{
if (MessageBox.Show("正在工作中,请问是否终止退出?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
ISOCreater.WorkState = false;
progressBar1.Value = 0;
client.CancelAsync();
this.Visible = false;
}
}
else
{
this.Visible = false;
}
}
private void txtFTPPath_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", txtFTPPath.Text);
}
static WebClient client = new WebClient();
#region Webclient的Fpt上传方式
public static void UploadFileInBackground2(string address, string fileName)
{
Uri uri = new Uri(address);
client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCallback2);
client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);
client.UploadFileAsync(uri, fileName);
Console.WriteLine("File upload started.");
}
static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
if (iCurPercent != e.ProgressPercentage)
{
iCurPercent = e.ProgressPercentage;
pg.Minimum = 0;
pg.Maximum = 100;
pg.Value = iCurPercent;
Console.WriteLine("{0} uploaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState, e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage);
}
}
private static void UploadFileCallback2(object sender, UploadFileCompletedEventArgs e)
{
TestResult = TimeSpan.FromTicks(DateTime.Now.Ticks - ExexcuteSeepStartTime).ToString();
tb.Text += "文件上传完毕!" + "\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n耗费时间:" + TestResult + "\r\n";
ISOCreater.WorkState = false;
pg.Value = 0;
tssl.Text = "等待处理";
Console.WriteLine("Over Make!");
}
static TextBox tb = null;
static Label tssl = null;
static ProgressBar pg = null;
static int iCurPercent = 0;
#endregion
private void testhost_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", txtFTPPath.Text);
}
private void FTPForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
if (progressBar1.Value >= progressBar1.Minimum && ISOCreater.WorkState == true)
{
if (MessageBox.Show("正在工作中,请问是否终止退出?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
ISOCreater.WorkState = false;
progressBar1.Value = 0;
client.CancelAsync();
this.Visible = false;
}
}
else
{
this.Visible = false;
}
}
private void FindFile_Click(object sender, EventArgs e)
{
this.openFileDialog1.DefaultExt = "ISO";
this.openFileDialog1.Filter = "ISO 标准格式|*.ISO|所有文件|*.*";
DialogResult dr = openFileDialog1.ShowDialog(this);
if (dr == DialogResult.OK)
{
lbfilename.Text = openFileDialog1.FileName;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -