지금 프로그레스바 동작하는게

전체 파일용량 체크해서 진행률을 보여줘야 하는데..

각 폴더별 파일용량을 체크해서 보여주는 것 같습니다...

100%가 여러번 차요...

전체용량에 대한 진행률을 보여주려면 어떻게 해야할까요...

구하는 식이 중간에 저렇게 쓰면 될 것 같았는데...

int percent = (int)((double)total_current / ((double)total * 100));

애초에 총 파일 용량을 구해오는 foreach문이 잘못된걸까요..?


 

private void Copy(string source, string dest)
        {
            FileInfo[] arrfileInfo = null;
            string[] dirList = Directory.GetDirectories(source);


            if (File.Exists(source))
            {
                arrfileInfo = new FileInfo[] { new FileInfo(source) };
            }
            else if (Directory.Exists(source))
            {
                DirectoryInfo di = new DirectoryInfo(source);
                arrfileInfo = di.GetFiles("*.*", SearchOption.AllDirectories);
            }

            foreach (string dir in dirList)
            {
                string dirName = dir.Substring(dir.LastIndexOf(@"") + 1);

                DirectoryInfo Dir = new DirectoryInfo(dest + dirName);

                if (!Dir.Exists)
                {
                    Directory.CreateDirectory(dest + dirName);
                }

                Copy(dir, dest + dirName + "");
            }

            if (arrfileInfo != null)
            {
                long total_current = 0;
                long total = 0;

                // 총 파일 용량
                foreach (FileInfo i in arrfileInfo)
                {
                    if ((i.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                        total += i.Length;
                }

                foreach (FileInfo i in arrfileInfo)
                {
                    if ((i.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    {
                        continue;
                    }

                    FileStream rs = null, ws = null;

                    try
                    {
                        rs = new FileStream(i.FullName, FileMode.Open);
                        ws = new FileStream(destPath + "//" + i.FullName.Substring(sourcePath.Length, i.FullName.Length - (i.FullName.Substring(0, sourcePath.Length).Length)), FileMode.Create);
                        //ws = new FileStream(dest + "//" + i.Name, FileMode.Create);

                        this.Invoke((SetLabelDelegate)delegate(string fileName)
                        {
                            lblFile.Text = fileName;
                        }, new object[] { i.Name });

                        int readSize = 0;

                        while ((readSize = rs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            total_current += readSize;

                            // ProgressBar
                            int percent = (int)((double)total_current / ((double)total * 100));

                            this.Invoke((SetProgDelegate)delegate(int value)
                            {
                                this.pbTransfer.Value = value;
                                this.lblPer.Text = String.Format("{0}%", value);
                            },
                            new object[] { percent });

                            ws.Write(buffer, 0, readSize);
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                    }
                    finally
                    {
                        try
                        {
                            rs.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                        try
                        {
                            ws.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                }
                this.Invoke((SetProgDelegate)delegate(int value)
                {
                    this.pbTransfer.Value = value;
                    this.lblPer.Text = String.Format("{0}%", value);
                }, new object[] { 100 });

                Thread.Sleep(500);
            }

            this.Invoke((ExitDelegate)delegate()
            {
                this.DoEnd();
            });
        }