Progressbar, backgroundworker (use class1.cs)

wonderland2001

New member
Joined
Apr 9, 2012
Messages
2
Programming Experience
Beginner
Nobody??

I do not know how to proceed..
And I do not know how the value of that class to which Form brings
well i got this now:
Form1 and class1.cs

C#:
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










    }
}




C#:
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;


        }
    }   
}

 
Top Bottom