C
Chocolade1972
Guest
In form1 i have two buttons events in one i can select singl file or multiple files:
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = true;
if (this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
this.txtUploadFile.Text = this.openFileDialog1.FileName;
FtpProgress.files = this.openFileDialog1.FileNames;
}
The second button is to select a directory:
private void button1_Click(object sender, EventArgs e)
{
if (this.folderBrowserDialog1.ShowDialog() != DialogResult.Cancel)
{
this.DirUploadTextBox.Text = this.folderBrowserDialog1.SelectedPath;
FtpProgress.d = new DirectoryInfo(this.folderBrowserDialog1.SelectedPath);
}
}
I also have in form1 textBoxes one to select multiple files then using the string[] and the second textBox to select the directory using DirectoryInfo and the FileInfo[]
txtUploadFile is the textBox im using to select single or multiple files.
And DirUploadTextBox im using to select the directory to upload.
Now i have the class that im using to upload file to ftp:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace FTP_ProgressBar
{
public partial class FtpProgress : BackgroundWorker
{
public static string ConnectionError;
public static string ftp;
private FtpSettings f;
public static DirectoryInfo d;
public static string[] files;
private FileInfo[] dirflist;
public FtpProgress()
{
InitializeComponent();
}
public FtpProgress(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private void FtpProgress_DoWork(object sender, DoWorkEventArgs e)
{
try
{
//dirflist = d.GetFiles();
//if (dirflist.Length > 0)
//{
foreach (string txf in files)
{
string fn = txf;//txf.Name;
BackgroundWorker bw = sender as BackgroundWorker;
f = e.Argument as FtpSettings;
string UploadPath = String.Format("{0}/{1}{2}", f.Host, f.TargetFolder == "" ? "" : f.TargetFolder + "/", Path.GetFileName(fn));//f.SourceFile));
if (!UploadPath.ToLower().StartsWith("ftp://"))
UploadPath = "ftp://" + UploadPath;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UploadPath);
request.UseBinary = true;
request.UsePassive = f.Passive;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = 300000;
request.Credentials = new NetworkCredential(f.Username, f.Password);
long FileSize = new FileInfo(f.SourceFile).Length;
string FileSizeDescription = GetFileSize(FileSize);
int ChunkSize = 4096, NumRetries = 0, MaxRetries = 50;
long SentBytes = 0;
byte[] Buffer = new byte[ChunkSize];
using (Stream requestStream = request.GetRequestStream())
{
using (FileStream fs = File.Open(f.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int BytesRead = fs.Read(Buffer, 0, ChunkSize);
while (BytesRead > 0)
{
try
{
if (bw.CancellationPending)
return;
requestStream.Write(Buffer, 0, BytesRead);
SentBytes += BytesRead;
string SummaryText = String.Format("Transferred {0} / {1}", GetFileSize(SentBytes), FileSizeDescription);
bw.ReportProgress((int)(((decimal)SentBytes / (decimal)FileSize) * 100), SummaryText);
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.ToString());
if (NumRetries++ < MaxRetries)
{
fs.Position -= BytesRead;
}
else
{
throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString()));
}
}
BytesRead = fs.Read(Buffer, 0, ChunkSize);
}
}
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
System.Diagnostics.Debug.WriteLine(String.Format("Upload File Complete, status {0}", response.StatusDescription));
}
//}
}
catch (WebException ex)
{
switch (ex.Status)
{
case WebExceptionStatus.NameResolutionFailure:
ConnectionError = "Error: Please check the ftp address";
break;
case WebExceptionStatus.Timeout:
ConnectionError = "Error: Timout Request";
break;
}
}
}
public static string GetFileSize(long numBytes)
{
string fileSize = "";
if (numBytes > 1073741824)
fileSize = String.Format("{0:0.00} Gb", (double)numBytes / 1073741824);
else if (numBytes > 1048576)
fileSize = String.Format("{0:0.00} Mb", (double)numBytes / 1048576);
else
fileSize = String.Format("{0:0} Kb", (double)numBytes / 1024);
if (fileSize == "0 Kb")
fileSize = "1 Kb";
return fileSize;
}
}
public class FtpSettings
{
public string Host, Username, Password, TargetFolder, SourceFile;
public bool Passive;
public int Port = 21;
}
}
I have a backgroundworker dowork event in this class and i want to use the code inside to decide if using FileInfo[] string[] or both.
In the top of the class i have the variables of the FileInfo[] and string[]
Then in the DoWork event for now im using the string[]
foreach (string txf in files)
{
string fn = txf;
This is what i need it to do:
1. If i select only sing file or multiple files to upload then use the foreach only for the string[] and upload each file one by one.
2. If i select a directory to upload then use the foreach with the FileInfo[] to upload the whole directory and the files inside.
a. If the directory not exist on the ftp server create the directory and if needed also subdirectories on the ftp server(using the f.TargetFolder) the checking is to create the directoy/subdirectories once and not ot create them all the time over again and run over existing files.
b. Upload the files and files in subdirectories to the ftp server to the directory and subdirectories.
3. If i select multiple files and also a directory then upload the multiple files and after it upload the directory.
This is a screenshot of my program:
The Target Folder textBox in form1 is for the Upload Files if i select a target folder and then select a singl file or multiple files then the files will be uploaded to the target folder(if target folder not exist on ftp server to create if exist to not create).
And if i select Upload Directory for example c:\temp
Then use also Target Directory(f.TargetFolder) and check if diorectory ,subdirectories,files already exist or not and upload the directory.
A lot of mess. In general i need to be able to decide if to upload only file/s or directory(with whole the content) or both.
Continue reading...
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = true;
if (this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
this.txtUploadFile.Text = this.openFileDialog1.FileName;
FtpProgress.files = this.openFileDialog1.FileNames;
}
The second button is to select a directory:
private void button1_Click(object sender, EventArgs e)
{
if (this.folderBrowserDialog1.ShowDialog() != DialogResult.Cancel)
{
this.DirUploadTextBox.Text = this.folderBrowserDialog1.SelectedPath;
FtpProgress.d = new DirectoryInfo(this.folderBrowserDialog1.SelectedPath);
}
}
I also have in form1 textBoxes one to select multiple files then using the string[] and the second textBox to select the directory using DirectoryInfo and the FileInfo[]
txtUploadFile is the textBox im using to select single or multiple files.
And DirUploadTextBox im using to select the directory to upload.
Now i have the class that im using to upload file to ftp:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace FTP_ProgressBar
{
public partial class FtpProgress : BackgroundWorker
{
public static string ConnectionError;
public static string ftp;
private FtpSettings f;
public static DirectoryInfo d;
public static string[] files;
private FileInfo[] dirflist;
public FtpProgress()
{
InitializeComponent();
}
public FtpProgress(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private void FtpProgress_DoWork(object sender, DoWorkEventArgs e)
{
try
{
//dirflist = d.GetFiles();
//if (dirflist.Length > 0)
//{
foreach (string txf in files)
{
string fn = txf;//txf.Name;
BackgroundWorker bw = sender as BackgroundWorker;
f = e.Argument as FtpSettings;
string UploadPath = String.Format("{0}/{1}{2}", f.Host, f.TargetFolder == "" ? "" : f.TargetFolder + "/", Path.GetFileName(fn));//f.SourceFile));
if (!UploadPath.ToLower().StartsWith("ftp://"))
UploadPath = "ftp://" + UploadPath;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UploadPath);
request.UseBinary = true;
request.UsePassive = f.Passive;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = 300000;
request.Credentials = new NetworkCredential(f.Username, f.Password);
long FileSize = new FileInfo(f.SourceFile).Length;
string FileSizeDescription = GetFileSize(FileSize);
int ChunkSize = 4096, NumRetries = 0, MaxRetries = 50;
long SentBytes = 0;
byte[] Buffer = new byte[ChunkSize];
using (Stream requestStream = request.GetRequestStream())
{
using (FileStream fs = File.Open(f.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int BytesRead = fs.Read(Buffer, 0, ChunkSize);
while (BytesRead > 0)
{
try
{
if (bw.CancellationPending)
return;
requestStream.Write(Buffer, 0, BytesRead);
SentBytes += BytesRead;
string SummaryText = String.Format("Transferred {0} / {1}", GetFileSize(SentBytes), FileSizeDescription);
bw.ReportProgress((int)(((decimal)SentBytes / (decimal)FileSize) * 100), SummaryText);
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.ToString());
if (NumRetries++ < MaxRetries)
{
fs.Position -= BytesRead;
}
else
{
throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString()));
}
}
BytesRead = fs.Read(Buffer, 0, ChunkSize);
}
}
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
System.Diagnostics.Debug.WriteLine(String.Format("Upload File Complete, status {0}", response.StatusDescription));
}
//}
}
catch (WebException ex)
{
switch (ex.Status)
{
case WebExceptionStatus.NameResolutionFailure:
ConnectionError = "Error: Please check the ftp address";
break;
case WebExceptionStatus.Timeout:
ConnectionError = "Error: Timout Request";
break;
}
}
}
public static string GetFileSize(long numBytes)
{
string fileSize = "";
if (numBytes > 1073741824)
fileSize = String.Format("{0:0.00} Gb", (double)numBytes / 1073741824);
else if (numBytes > 1048576)
fileSize = String.Format("{0:0.00} Mb", (double)numBytes / 1048576);
else
fileSize = String.Format("{0:0} Kb", (double)numBytes / 1024);
if (fileSize == "0 Kb")
fileSize = "1 Kb";
return fileSize;
}
}
public class FtpSettings
{
public string Host, Username, Password, TargetFolder, SourceFile;
public bool Passive;
public int Port = 21;
}
}
I have a backgroundworker dowork event in this class and i want to use the code inside to decide if using FileInfo[] string[] or both.
In the top of the class i have the variables of the FileInfo[] and string[]
Then in the DoWork event for now im using the string[]
foreach (string txf in files)
{
string fn = txf;
This is what i need it to do:
1. If i select only sing file or multiple files to upload then use the foreach only for the string[] and upload each file one by one.
2. If i select a directory to upload then use the foreach with the FileInfo[] to upload the whole directory and the files inside.
a. If the directory not exist on the ftp server create the directory and if needed also subdirectories on the ftp server(using the f.TargetFolder) the checking is to create the directoy/subdirectories once and not ot create them all the time over again and run over existing files.
b. Upload the files and files in subdirectories to the ftp server to the directory and subdirectories.
3. If i select multiple files and also a directory then upload the multiple files and after it upload the directory.
This is a screenshot of my program:
The Target Folder textBox in form1 is for the Upload Files if i select a target folder and then select a singl file or multiple files then the files will be uploaded to the target folder(if target folder not exist on ftp server to create if exist to not create).
And if i select Upload Directory for example c:\temp
Then use also Target Directory(f.TargetFolder) and check if diorectory ,subdirectories,files already exist or not and upload the directory.
A lot of mess. In general i need to be able to decide if to upload only file/s or directory(with whole the content) or both.
Continue reading...