using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using TusharFtp;
using System.Configuration;
using System.Runtime.InteropServices;
namespace ftp_progress_bar
{
public partial class Form1 : Form
{
[DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static void ShowToFront(string windowName)
{
IntPtr firstInstance = FindWindow(null, windowName);
ShowWindow(firstInstance, 1);
SetForegroundWindow(firstInstance);
}
public Form1()
{
InitializeComponent();
}
private int c;
public void tushar1(long a, long b)
{
MethodInvoker m = delegate
{
label1.Text = b + " out of " + a + " left " + (++c) + "%";
if (c <= 100)
{
progressBar1.Value = c;
}
};
this.BeginInvoke(m);
}
private string users, pass, servers, dirs;
private void tushar()
{
try
{
MethodInvoker m1 = delegate
{
button2.Text = "uploading";
};
this.BeginInvoke(m1);
TusharFtp.FtpClass t = new TusharFtp.FtpHelperClass();
t.FtpServer = servers;
t.FtpUserName = users;
t.FtpPassword = pass;
t.FtpModeUsePassive = true;
t.uploadFile += new TusharFtp.DisplayUploadDelegate(tushar1);
c = 0;
string h = targets.ToString();
string a = t.FtpFileUpload(targets.ToString(), dirs);
if (a.IndexOf("226 ") != -1)
{try{
MethodInvoker m = delegate
{
string d= dirs.Replace("/","\\");
d = d.Replace("\\httpdocs\\", "\\");
label1.Text = targets.Name + " has been succesfully uploaded";
richTextBox1.AppendText("\n http:\\\\" + t.FtpServer + d + targets.Name);
progressBar1.Value = 100;
button2.Text = "upload";
textBox1.Text = string.Empty;
};
this.BeginInvoke(m);
}
catch(InvalidOperationException ins)
{
button2.Text = "upload";
textBox1.Text = string.Empty;
}
}
else
{
try
{
MethodInvoker m = delegate
{
label1.Text = a;
button2.Text = "upload";
textBox1.Text = string.Empty;
};
this.BeginInvoke(m);
}
catch(InvalidOperationException)
{
button2.Text = "upload";
}
}
}
catch (NullReferenceException )
{
MessageBox.Show("some fields are left empty","error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private FileInfo targets;
private void button1_Click(object sender, EventArgs e)
{
if (button2.Text == "uploading" && textBox1.Text != string.Empty)
{
MessageBox.Show("wait for the upload to complete!", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
targets = new FileInfo(textBox1.Text);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
users = ConfigurationManager.AppSettings["usersname"].ToString();
pass= ConfigurationManager.AppSettings["userspass"].ToString();
servers = ConfigurationManager.AppSettings["userserver"].ToString();
dirs = ConfigurationManager.AppSettings["userdir"].ToString();
if (button2.Text == "upload" && textBox1.Text!=string.Empty)
{
ThreadStart th = new ThreadStart(tushar);
System.Threading.Thread t1 = new System.Threading.Thread(th);
t1.Start();
}
else if (button2.Text == "uploading" && textBox1.Text != string.Empty)
{
MessageBox.Show("wait for the upload to complete!", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show("some fields are empty!", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
button2.Text = "upload";
}
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Show();
this.ShowInTaskbar = true;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
components.Dispose();
base.Dispose(true);
}
private void options(object sender, EventArgs e)
{
optionsdialog opts = new optionsdialog();
opts.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
components.Dispose();
base.Dispose(true);
}
}
}