コード例 #1
0
        public static void CopyTo(this FileInfo file, FileInfo destination, Update_Status_Delegate progressCallback)
        {
            const int bufferSize = 1024 * 1024;  //1MB

            byte[] buffer = new byte[bufferSize], buffer2 = new byte[bufferSize];
            bool   swap = false;
            int    progress = 0, reportedProgress = 0, read = 0;
            long   len    = file.Length;
            float  flen   = len;
            Task   writer = null;

            using (var source = file.OpenRead())
                using (var dest = destination.OpenWrite())
                {
                    dest.SetLength(source.Length);
                    for (long size = 0; size < len; size += read)
                    {
                        if ((progress = ((int)((size / flen) * 100))) != reportedProgress)
                        {
                            progressCallback(reportedProgress = progress);
                        }
                        read = source.Read(swap ? buffer : buffer2, 0, bufferSize);
                        writer?.Wait(); // if < .NET4 // if (writer != null) writer.Wait();
                        writer = dest.WriteAsync(swap ? buffer : buffer2, 0, read);
                        swap   = !swap;
                    }
                    writer?.Wait(); //Fixed - Thanks @sam-hocevar
                }
        }
コード例 #2
0
ファイル: FormMain.cs プロジェクト: baovit72/visual-explorer
        private void menuPaste_Click(object sender, EventArgs e)
        {
            try
            {
                string pathSource, pathDest, pathString;
                //MessageBox.Show(folderName);
                if (isListView)
                {
                    if (isFolder)                                              // Nếu là folder
                    {
                        pathSource = pathFolder;                               // Lưu path nguồn
                        pathString = Path.Combine(currentPath, pasteItemName); // Lưu path
                        Directory.CreateDirectory(pathString);                 // Tạo thư mục ở vị trí mới có tên folder muốn paste
                        pathDest = pathString;                                 // Lưu path dích
                    }
                    else
                    {
                        pathSource = pathFile;
                        pathDest   = currentPath + itemPaste.Text;
                    }
                }
                else
                {
                    pathSource = pathNode;
                    pathString = Path.Combine(currentPath, pasteItemName);
                    Directory.CreateDirectory(pathString);
                    pathDest = pathString;
                }
                //copy
                if (isCopying)
                {
                    if (isFolder)
                    {
                        Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(pathSource, pathDest, true);
                    }
                    // Copy thư mục
                    //{
                    //    if (Directory.Exists(pathSource))
                    //    {
                    //        string[] files = Directory.GetFiles(pathSource);

                    //        foreach (string s in files)
                    //        {
                    //            string fileName = Path.GetFileName(s);
                    //            string destFile = Path.Combine(pathDest, fileName);
                    //            File.Copy(s, destFile, true);
                    //        }
                    //    }
                    //    else
                    //    {
                    //        Console.WriteLine("Source path does not exist!");
                    //    }
                    //}
                    else
                    {
                        FileInfo _source      = new FileInfo(pathSource);
                        FileInfo _destination = new FileInfo(pathDest);
                        if (_destination.Exists)
                        {
                            _destination.Delete();
                        }
                        Update_Status_Delegate actionUpdate = new Update_Status_Delegate(Update_Status);
                        CopyUtils.CopyTo(_source, _destination, actionUpdate);
                    }
                    // Copy file
                    //File.Copy(pathSource, pathDest, true);
                    label1.Text = "Done";
                    isCopying   = false;
                }
                //cut
                if (isCutting)
                {
                    if (isFolder)
                    {
                        Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(pathSource, pathDest, true); // Move thư mục
                    }
                    else
                    //    Microsoft.VisualBasic.FileIO.FileSystem.MoveFile(pathSource, pathDest, true); // Move file
                    {
                        FileInfo _source      = new FileInfo(pathSource);
                        FileInfo _destination = new FileInfo(pathDest);
                        if (_destination.Exists)
                        {
                            _destination.Delete();
                        }
                        Update_Status_Delegate actionUpdate = new Update_Status_Delegate(Update_Status);
                        CopyUtils.CopyTo(_source, _destination, actionUpdate);
                    }
                    label1.Text = "Done";
                    isCutting   = false;
                }
                string strPath;
                if (!isFolder)
                {
                    strPath = clsTreeListView.GetPathDir(pathDest);
                }
                else
                {
                    strPath = currentPath;
                }
                clsTreeListView.FocusItem(listView, strPath, pasteItemName); // Hiện thư mục và select item vừa move/copy
                menuPaste.Enabled  = false;
                tsbtnPaste.Enabled = false;
                if (listView.Items.Count > 0)
                {
                    statusLabel.Text = listView.Items.Count.ToString() + " đối tượng";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }