Пример #1
0
        private void Copy(string fromFile, string toFile, long transferedSize)
        {
            if (!File.Exists(fromFile))
            {
                throw new Exception("Input File " + fromFile + "Can't Opened.");
            }

            if (File.Exists(toFile))
            {
                if (isNoOverwriteAll_)
                {
                    return;
                }
                if (!isOverwriteAll_)
                {
                    OverwritePrompt opWnd = new OverwritePrompt();
                    OverwriteOption owOp  = opWnd.ExecuteDialog("File " + toFile + " was exists. Overwrite it?");
                    if (owOp == OverwriteOption.NoToAll)
                    {
                        isNoOverwriteAll_ = true;
                        return;
                    }
                    if (owOp == OverwriteOption.No)
                    {
                        return;
                    }
                    if (owOp == OverwriteOption.YesToAll)
                    {
                        isOverwriteAll_ = true;
                    }
                }
            }
            FileStream fin  = null;
            FileStream fout = null;

            try
            {
                string outDirectory = Path.GetDirectoryName(toFile);
                if (!Directory.Exists(outDirectory))
                {
                    Directory.CreateDirectory(outDirectory);
                }
                fin  = new FileStream(fromFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                fout = new FileStream(toFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);

                int    bufsize = 64 * 1024;              // 64K Buffer
                byte[] buf     = new byte[bufsize];

                long curPos = 0;
                while (true)
                {
                    if (isNeedExit_)
                    {
                        throw new Exception(
                                  "File copy was aborted by user."
                                  );
                    }

                    int readCount = fin.Read(buf, 0, bufsize);
                    fout.Write(buf, 0, readCount);
                    curPos += readCount;

                    //设置进度条和时间
                    pbTotal.Value = (int)((double)(curPos + transferedSize) / (double)totalSize_ * 1000.0);
                    pbFile.Value  = (int)((double)curPos / (double)fin.Length * 1000.0);
                    int thisTick    = Environment.TickCount;
                    int elapsedTick = thisTick - startTick_;
                    int totalTick   = (int)((double)elapsedTick / (double)(curPos + transferedSize) * (double)totalSize_);

                    TimeSpan ts = new TimeSpan(elapsedTick / 1000);
                    elapsedTimeStr_ = ts.ToString();
                    ts = new TimeSpan(totalTick - elapsedTick);
                    residualTimeStr_ = ts.ToString();

                    lblFilePercentage.Text  = (pbFile.Value / 10).ToString() + "%";
                    lblTotalPercentage.Text = (pbTotal.Value / 10).ToString() + "%";

                    Application.DoEvents();

                    if (readCount < bufsize)
                    {
                        break;
                    }
                }
            }
            catch (Exception exc)
            {
                if (fin != null)
                {
                    fin.Close();
                }
                if (fout != null)
                {
                    fout.Close();
                }
                try
                {
                    File.Delete(toFile);
                }
                catch
                {
                    ;                     //null
                }
                throw exc;
            }
            finally
            {
                fin.Close();
                fout.Close();

                FileInfo srcFileInfo = new FileInfo(fromFile);
                FileInfo toFileInfo  = new FileInfo(toFile);

                toFileInfo.LastWriteTime = srcFileInfo.LastWriteTime;
            }
        }
Пример #2
0
        private void Copy( string fromFile, string toFile, long transferedSize )
        {
            if( !File.Exists( fromFile ) ) {
                throw new Exception( "Input File " + fromFile + "Can't Opened." );
            }

            if( File.Exists( toFile ) ) {
                if( isNoOverwriteAll_ ) {
                    return;
                }
                if( !isOverwriteAll_ ) {
                    OverwritePrompt opWnd = new OverwritePrompt();
                    OverwriteOption owOp = opWnd.ExecuteDialog( "File " + toFile + " was exists. Overwrite it?" );
                    if( owOp == OverwriteOption.NoToAll ) {
                        isNoOverwriteAll_ = true;
                        return;
                    }
                    if( owOp == OverwriteOption.No ) {
                        return;
                    }
                    if( owOp == OverwriteOption.YesToAll ) {
                        isOverwriteAll_ = true;
                    }
                }
            }
            FileStream fin = null;
            FileStream fout = null;
            try
            {
                string outDirectory = Path.GetDirectoryName(toFile);
                if(!Directory.Exists(outDirectory)){
                    Directory.CreateDirectory(outDirectory);
                }
                fin = new FileStream(fromFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                fout = new FileStream(toFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);

                int bufsize = 64 * 1024; // 64K Buffer
                byte[] buf = new byte[bufsize];

                long curPos = 0;
                while (true)
                {
                    if (isNeedExit_)
                    {
                        throw new Exception(
                            "File copy was aborted by user."
                            );
                    }

                    int readCount = fin.Read(buf, 0, bufsize);
                    fout.Write(buf, 0, readCount);
                    curPos += readCount;

                    //���ý�������ʱ��
                    pbTotal.Value = (int)((double)(curPos + transferedSize) / (double)totalSize_ * 1000.0);
                    pbFile.Value = (int)((double)curPos / (double)fin.Length * 1000.0);
                    int thisTick = Environment.TickCount;
                    int elapsedTick = thisTick - startTick_;
                    int totalTick = (int)((double)elapsedTick / (double)(curPos + transferedSize) * (double)totalSize_);

                    TimeSpan ts = new TimeSpan(elapsedTick / 1000);
                    elapsedTimeStr_ = ts.ToString();
                    ts = new TimeSpan(totalTick - elapsedTick);
                    residualTimeStr_ = ts.ToString();

                    lblFilePercentage.Text = (pbFile.Value / 10).ToString() + "%";
                    lblTotalPercentage.Text = (pbTotal.Value / 10).ToString() + "%";

                    Application.DoEvents();

                    if (readCount < bufsize)
                    {
                        break;
                    }
                }
            }
            catch (Exception exc)
            {
                if (fin != null)
                {
                    fin.Close();
                }
                if (fout != null)
                {
                    fout.Close();
                }
                try
                {
                    File.Delete(toFile);
                }
                catch
                {
                    ; //null
                }
                throw exc;
            }
            finally
            {
                fin.Close();
                fout.Close();

                FileInfo srcFileInfo = new FileInfo(fromFile);
                FileInfo toFileInfo = new FileInfo(toFile);

                toFileInfo.LastWriteTime = srcFileInfo.LastWriteTime;
            }
        }