private void pasteToolStripMenuItem_Click(object sender, EventArgs e) { if (!Clipboard.ContainsFileDropList()) { return; } int?idWhere = this.idCurrentFolder; if (!idWhere.HasValue) { return; } int nFiles = 0; long nBytes = 0; Cursor.Current = Cursors.WaitCursor; Stopwatch swAll = new Stopwatch(); List <EfsEntry> newEntries = new List <EfsEntry>(); swAll.Start(); using (var trans = sess.BeginTransaction()) { foreach (string strPath in Clipboard.GetFileDropList().Cast <string>()) { EfsEntry ne = null; if (File.Exists(strPath)) // Single file { nFiles++; long len; ne = EFS.AddFile(rs, idWhere.Value, fileIo, strPath, out len); nBytes += len; trans.LazyCommitAndReopen(); } else if (Directory.Exists(strPath)) { ne = AddFolder(trans, idWhere.Value, strPath, ref nFiles, ref nBytes); } if (null != ne) { newEntries.Add(ne); } } trans.Commit(); swAll.Stop(); } Cursor.Current = Cursors.Default; newEntries.ForEach(ne => this.AddUiItem(ne)); string msg = FormatCopyRateSummary(nFiles, nBytes, swAll.Elapsed); MessageBox.Show(this, msg, "Paste Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); }
EfsEntry AddFolder(iSerializerTransaction trans, int idParent, string strPath, ref int nFiles, ref long cbTotalBytes) { string name = Path.GetFileName(strPath); int? idFile = EFS.FindFile(rs, idParent, name); if (idFile.HasValue) { EFS.DeleteFile(rs, idFile.Value); trans.LazyCommitAndReopen(); } EfsEntry res = EFS.CreateFolder(rs.cursor, idParent, name); idParent = res.id; // Inspired by http://stackoverflow.com/questions/2085452/fast-lowlevel-method-to-recursively-process-files-in-folders/2085872#2085872 var pending = new Queue <Tuple <string, int> >(); pending.Enqueue(Tuple.Create(strPath, idParent)); string[] tmp; while (pending.Count > 0) { Tuple <string, int> p = pending.Dequeue(); tmp = Directory.GetDirectories(p.Item1); foreach (var childFolder in tmp) { name = Path.GetFileName(childFolder); idParent = EFS.CreateFolder(rs.cursor, p.Item2, name).id; pending.Enqueue(Tuple.Create(childFolder, idParent)); trans.LazyCommitAndReopen(); } tmp = Directory.GetFiles(p.Item1); foreach (var filePath in tmp) { nFiles++; long len; EFS.AddFile(rs, p.Item2, fileIo, filePath, out len); cbTotalBytes += len; trans.LazyCommitAndReopen(); } } return(res); }