 |
 |

04-09-2012, 03:06 AM
|
|
Newcomer
|
|
Join Date: Mar 2009
Posts: 3
|
|
progressbar, backgroundworker (use a class1.cs)
|
Hi,
My program:
I download (class1.cs) files from an FTP server.
This I want to see a progressbar.
Because I use a class1.cs is I use a background worker
Here is my project. c#
https://rapidshare.com/files/2905061...Downloader.zip
If someone can put that in my project.
Thank you!
Jacky
|
|

04-09-2012, 06:14 AM
|
|
Newcomer
|
|
Join Date: Mar 2009
Posts: 3
|
|
The problem is to show a progressbar when he is downloading the files
i don't know how
here some code:
Form:
Code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public string pathName;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 formke2 = new Form2();
formke2.Show();
Class1.pathName=textBox1.Text;
Class1.CombineBlocksIntoLibrary(Class1.getpathName());
}
}
}
Class1.cs
Code:
namespace WindowsFormsApplication1
{
class Class1
{
public static String pathName;
public static String getpathName()
{
return pathName;
}
public static void CombineBlocksIntoLibrary(string pathName)
{
string activeDir = @"c:\Temp";
//Create a new subfolder under the current active folder
string newPath = System.IO.Path.Combine(activeDir, "test");
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
//File Download,Write Local Copy, from FTP Location
string localPath = @"C:\Temp\test\";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pathName);
request.Credentials = new NetworkCredential("LOGIN", "PASSWORD");
request.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
request = null;
string fileName = streamReader.ReadLine();
while (fileName != null)
{
FtpWebRequest requestFileDownload = null;
FtpWebResponse responseFileDownload = null;
try
{
Console.WriteLine(fileName);
requestFileDownload = (FtpWebRequest)WebRequest.Create(pathName + fileName);
requestFileDownload.Credentials = new NetworkCredential("LOGIN", "PASSWORD");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
writeStream.Close();
Console.WriteLine("Download Done");
}
catch (System.Exception exceptionObj)
{
Console.WriteLine(exceptionObj.Message.ToString());
}
finally
{
requestFileDownload = null;
responseFileDownload = null;
}
fileName = streamReader.ReadLine();
}
request = null;
streamReader = null;
}
}
}
|
|

04-10-2012, 04:55 AM
|
|
Newcomer
|
|
Join Date: Mar 2009
Posts: 3
|
|
Nobody a idea??
this is a update:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//add form Form msdn
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public string pathName;
private BackgroundWorker bgw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Class1.pathName=textBox1.Text;
Class1.CombineBlocksIntoLibrary(Class1.getpathName());
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerAsync();
}
#region [ Threading ]
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
/*
//get the total of files (number of them, or an array of them (like FileInfo[])
for (i = 0; i < totalFiles; i++)
{
//do what ever with the single file...
//and pass the value to progressbar:
bgw.ReportProgress(i);
}*/
}
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Reset the progress bar
prbProgress.Minimum = 0;
prbProgress.Value = 0;
prbProgress.Enabled = false;
}
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
prbProgress.Value = e.ProgressPercentage;
}
#endregion
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace WindowsFormsApplication1
{
class Class1
{
public static String pathName;
public static String getpathName()
{
return pathName;
}
public static void CombineBlocksIntoLibrary(string pathName)
{
string activeDir = @"c:\Temp";
//Create a new subfolder under the current active folder
string newPath = System.IO.Path.Combine(activeDir, "test");
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
//File Download,Write Local Copy, from FTP Location
string localPath = @"C:\Temp\test\";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pathName);
request.Credentials = new NetworkCredential("LOGIn", "PASSWORD");
request.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
request = null;
string fileName = streamReader.ReadLine();
while (fileName != null)
{
FtpWebRequest requestFileDownload = null;
FtpWebResponse responseFileDownload = null;
try
{
Console.WriteLine(fileName);
requestFileDownload = (FtpWebRequest)WebRequest.Create(pathName + fileName);
requestFileDownload.Credentials = new NetworkCredential("LOGIN", "PASSWORD");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
Int64 iRunningByteTotal = 0;
iRunningByteTotal += bytesRead;
// calculate the progress out of a base "100"
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)buffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
writeStream.Close();
Console.WriteLine("Download Done");
}
catch (System.Exception exceptionObj)
{
Console.WriteLine(exceptionObj.Message.ToString());
}
finally
{
requestFileDownload = null;
responseFileDownload = null;
}
fileName = streamReader.ReadLine();
}
request = null;
streamReader = null;
}
}
}
|
|

04-10-2012, 05:25 AM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,882
|
|
|
This is a forum dedicated to Visual Basic (.Net), I guess that's the reasons for your non response.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|