Пример #1
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);
            }
        }
Пример #2
0
        // User Press process button
        public void StartOrStop(object sender, EventArgs e)
        {
            ProgressBar1.Value = 0;
            if (bw.IsBusy != true)
            {
                //start processing
                //Busy = true;

                messageLabel1.Text = "Starting...";
                if (listView1.Items.Count <= 0)
                {
                    MessageBox.Show("No files can be processed!!");
                    messageLabel1.Text = "No files!";
                    return;
                }
                else if (listView1.Items.Count == 1)
                {
                    MessageBox.Show("It is not necessary to process files when there is only one file!!");
                    messageLabel1.Text = "1 file!";
                    return;
                }
                else
                {
                    //summary size and compare to free space
                    long totalSize = 0;
                    long maxSize   = 0;
                    foreach (ListViewItem lvi in listView1.Items)
                    {
                        long size = long.Parse(lvi.SubItems[4].Text);
                        if (size > maxSize)
                        {
                            maxSize = size;
                        }

                        totalSize += size;
                    }
                    if (totalSize > diskFree)
                    {
                        MessageBox.Show(string.Format("Not enought free space , you need total free space large then {0}", totalSize));
                        messageLabel1.Text = "space not enought!";
                        return;
                    }
                }

                startButton1.Image = MakeFileOrder.Properties.Resources.stop_241;
                startButton2.Image = MakeFileOrder.Properties.Resources.stop_241;

                List <FileList> fileLists = new List <FileList>();
                foreach (ListViewItem lvi in listView1.Items)
                {
                    FileList fl = new FileList();
                    fl.FileName = lvi.SubItems[0].Text;
                    fileLists.Add(fl);
                }
                PassArguments pa = new PassArguments();
                pa.Files             = fileLists;
                pa.SourceFolder      = workFolder;
                pa.DestinationFolder = driveLabel1.Text + "tempF";

                bw.RunWorkerAsync(pa);

                // TESTING CASE
                //if (!Directory.Exists(pa.DestinationFolder))
                //{
                //    Directory.CreateDirectory(pa.DestinationFolder);
                //}
                //int fcount = 0;
                //foreach (FileList file in pa.Files)
                //{
                //    fcount++;
                //    string file1 = pa.SourceFolder + "\\" + file.FileName;
                //    string file2 = pa.DestinationFolder + "\\" + file.FileName;
                //    messageLabel1.Text = ">>" + file.FileName;
                //    //bw.ReportProgress((fcount / pa.Files.Count) * 100, "file.FileName");
                //    using (FileStream fs1 = File.Open(file1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                //    {
                //        using (BufferedStream bs1 = new BufferedStream(fs1))
                //        {
                //            using (BinaryReader br = new BinaryReader(bs1))
                //            {
                //                FileStream fs2 = File.Open(file2, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                //                BufferedStream bs2 = new BufferedStream(fs2);
                //                BinaryWriter bw = new BinaryWriter(bs2);
                //                byte[] data;
                //                while (br.BaseStream.Position != br.BaseStream.Length)
                //                {
                //                    data = br.ReadBytes(4096);
                //                    bw.Write(data);
                //                }
                //                bw.Close();
                //                bw.Dispose();
                //                bs2.Close();
                //                bs2.Dispose();
                //                fs2.Close();
                //                fs2.Dispose();
                //            }
                //        }
                //    }
                //}
            }
            else
            {
                //break processing
                //Busy = false;

                if (bw.CancellationPending == true)
                {
                    bw.CancelAsync();
                    messageLabel1.Text = "User Cancalling.....";
                }
            }
        }