Here it goes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using Microsoft;
namespace WebBackup
{
public partial class frmBackup : Form
{
private string huidigWeb;
private string huidigMap;
private WebBackupForm f1 = new WebBackupForm();
public frmBackup(WebBackupForm F1)
{
f1 = F1;
InitializeComponent();
}
public WebBackupForm F1
{
get { return f1; }
}
/// <summary>
/// Method to upload the specified file to the specified FTP Server
/// </summary>
/// <param name="filename">file full name to be uploaded</param>
///
/// filename = local
/// FTnaam = FTP naam
private void Upload(string filename, string FTnaam)
{
// MessageBox.Show("in upload.");
FileInfo fileInf = new FileInfo(filename);
// MessageBox.Show("file: " + filename + " upload: "
+ FTnaam);
string uri = "ftp://" + F1.FTPserver + "/"; // fileInf.Name;
FtpWebRequest reqFTP;
MessageBox.Show("Uri :" + uri);
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
//"ftp://" + F1.FTPserver + "/" + FTnaam));
// MessageBox.Show("ReqFTP");
//fileInf.Name));
MessageBox.Show("After uri");
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(F1.FTPnaam,
F1.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;
// Calculate the progressbar
long len = fileInf.Length;
len = len / 2048;
if (len > 0)
len = 1000 / len;
else len = 1;
if (len <= 0)
len = 1;
int max = (int)len;
// Show count
long Teller = fileInf.Length;
lblTeller.Text = Teller.ToString();
// The buffer size is set to 2kb
int buffLength = 2048;
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();
this.lblBestand.Text = FTnaam;
// try
// {
// 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);
// 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);
progressBar.Value = (int)len;
progressBar.PerformStep();
len += max;
if (len >= 1000) len = 0;
Teller -= (long)contentLen;
lblTeller.Text = Teller.ToString();
this.Refresh();
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
public void DeleteFTP
(string
fileName)
{
try
{
string uri = "ftp://" + F1.FTPserver + "/" + fileName;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new
Uri("ftp://" + F1.FTPserver + "/" + fileName));
reqFTP.Credentials = new NetworkCredential(F1.FTPnaam,
F1.FTPpassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
string result = String.Empty;
FtpWebResponse response =
(FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message, "FTP 2.0
Delete");
}
}
private
void makedir
(string
dirName)
{
/* string dir = dirName.Replace('/', '\\');
dir = "H:\\" + dir;
Directory.CreateDirectory(dir);
return;
*/
// MessageBox.Show("In makedir.");
FtpWebRequest reqFTP;
try
{
// dirName = name of the directory to create.
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new
Uri("ftp://" + F1.FTPserver + "/" + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(F1.FTPnaam,
F1.FTPpassword);
FtpWebResponse response =
(FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message, " Directory fout");
}
}
private
void btnAnnuleer_Click
(object
sender,
EventArgs e)
{
this.Close();
}
private
void btnStart_Click
(object
sender,
EventArgs e)
{
int i;
int Positie = 0;
string Mappen = "";
string WebMappen = "";
string backup = "/backup/";
lblBackup.Text = "Bezig met de backup naar " + F1.FTPserver;
this.Refresh();
DeleteFTP("backup/");
// makedir(backup);
// Ga dooe de lijst heen van mappen die door de gebruiker
aangeklikt zijn om te
// backuppen
for (i = 0; i < F1.BestandsLijst.Items.Count; i++)
{
Mappen = F1.BestandsLijst.Items.ToString();
Positie = Mappen.IndexOf('\\');
WebMappen = backup;
WebMappen += Mappen.Substring(Positie + 1);
WebMappen = WebMappen.Replace('\\', '/');
string dirHulp = "";
// Maak eerst de mappen die niet met bestanden moetn worden
gechreven
foreach (string dirPart in WebMappen.Split('/'))
{
dirHulp += dirPart;
dirHulp += "/";
if (dirHulp.Length > 1)
{
makedir(dirHulp);
}
}
DirectoryInfo dirinf = new DirectoryInfo(Mappen);
huidigWeb = dirHulp;
huidigMap = Mappen;
schrijfNaarWeb(Mappen);
// string[] bestanden = GetFileList();
}
lblBestand.Text = "Klaar met uploaden.";
this.Refresh();
}
public
void schrijfNaarWeb
(string
directory)
{
if (!Directory.Exists(directory))
{
throw new FileNotFoundException("De map bestaat niet. " +
directory);
}
try
{
string[] files = Directory.GetFiles(directory);
foreach (string file in files)
{
if (file != null)
{
string copyfrom = file;
string copyto;
string webfile = file.Replace('\\', '/');
int wfpos = webfile.IndexOf('/');
webfile = "/backup/" + webfile.Substring(wfpos + 1);
copyto = webfile.Replace('/', '\\');
copyto = "H:\\" + copyto;
lblBackup.Text = copyfrom;
this.Refresh();
// File.Copy(copyfrom, copyto);
Upload(file, webfile);
}
}
string[] dirs = Directory.GetDirectories(directory);
foreach (string dir in dirs)
{
string webdir = dir.Replace('\\', '/');
int wdpos = webdir.IndexOf('/');
webdir = "/backup/" + webdir.Substring(wdpos + 1);
makedir(webdir);
schrijfNaarWeb(dir);
}
}
catch (Exception ex)
{
MessageBox.Show("Exeception :" + ex.Message);
}
}
}
}