Example #1
0
        private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            ReturnArguments ri = (ReturnArguments)e.UserState;

            if (ri.Code == 1)
            {
                ProgressBar1.Value = e.ProgressPercentage;
                messageLabel1.Text = ri.FileName + " to Processing..";
                ProgressBar2.Value = 0;
            }
            else if (ri.Code == 2)
            {
                ProgressBar2.Value = e.ProgressPercentage;
            }
        }
Example #2
0
        private void ProcessGo(object sender, DoWorkEventArgs e)
        {
            PassArguments pa = (PassArguments)e.Argument;

            if (!Directory.Exists(pa.DestinationFolder))
            {
                Directory.CreateDirectory(pa.DestinationFolder);
            }
            int fcount = 0;

            foreach (FileList file in pa.Files)
            {
                ReturnArguments ra = new ReturnArguments();
                ra.FileName = file.FileName;
                ra.Code     = 1;
                fcount++;
                string file1 = pa.SourceFolder + "\\" + file.FileName;
                string file2 = pa.DestinationFolder + "\\" + file.FileName;
                bw.ReportProgress((int)(((float)fcount / (float)pa.Files.Count) * 50.0), ra);
                //Thread.Sleep(10000);
                //File.Copy(file1, file2);
                using (FileStream fs1 = File.Open(file1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (BufferedStream bs1 = new BufferedStream(fs1))
                    {
                        using (BinaryReader br = new BinaryReader(bs1))
                        {
                            ReturnArguments ra2 = new ReturnArguments();
                            ra2.Code = 2;
                            FileStream     fs2   = File.Open(file2, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                            BufferedStream bs2   = new BufferedStream(fs2);
                            BinaryWriter   binwr = new BinaryWriter(bs2);
                            byte[]         data;
                            while (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                data = br.ReadBytes(4096);
                                binwr.Write(data);
                                bw.ReportProgress((int)(((float)br.BaseStream.Position / (float)br.BaseStream.Length) * 100.0), ra2);

                                if (e.Cancel == true)
                                {
                                    break;
                                }
                            }
                            binwr.Close();
                            binwr.Dispose();
                            bs2.Close();
                            bs2.Dispose();
                            fs2.Close();
                            fs2.Dispose();
                        }
                    }
                }
                if (e.Cancel == true)
                {
                    break;
                }
            }

            // if cancel ,delete all temp files & temp directory
            if (e.Cancel == true)
            {
                foreach (string file in Directory.GetFiles(pa.DestinationFolder))
                {
                    File.Delete(file);
                }
                Directory.Delete(pa.DestinationFolder);
            }
            else
            {
                // otherwise delete source directory files and move all temp files to source directory
                // in this step will not allow cacel command
                string[] files  = Directory.GetFiles(pa.DestinationFolder);
                int      count1 = 0;
                int      count2 = files.Length;
                foreach (string file in files)
                {
                    count1++;
                    ReturnArguments ra = new ReturnArguments();
                    ra.Code     = 1;
                    ra.FileName = file;
                    string fileName     = Path.GetFileName(file);
                    string destFileName = pa.SourceFolder + "\\" + fileName;
                    File.Delete(destFileName);
                    File.Move(file, destFileName);
                    bw.ReportProgress((int)(((float)count1 / (float)count2) * 50.0) + 50, ra);
                }
                Directory.Delete(pa.DestinationFolder);
            }
        }