Go Back  Xtreme Visual Basic Talk > Other Languages > Miscellaneous Languages > progressbar, backgroundworker (use a class1.cs)


Reply
 
Thread Tools Display Modes
  #1  
Old 04-09-2012, 03:06 AM
wonderland wonderland is offline
Newcomer
 
Join Date: Mar 2009
Posts: 3
Default 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
Reply With Quote
  #2  
Old 04-09-2012, 06:14 AM
wonderland wonderland is offline
Newcomer
 
Join Date: Mar 2009
Posts: 3
Default

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;

        }
    }   
}
Reply With Quote
  #3  
Old 04-10-2012, 04:55 AM
wonderland wonderland is offline
Newcomer
 
Join Date: Mar 2009
Posts: 3
Default

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;


        }
    }   
}
Reply With Quote
  #4  
Old 04-10-2012, 05:25 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,052
Default

This is a forum dedicated to Visual Basic (.Net), I guess that's the reasons for your non response.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->