Programming C#, 4th Edition by Jesse Liberty
Author:Jesse Liberty [Jesse Liberty]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / C#
ISBN: 9780596552725
Publisher: O'Reilly Media
Published: 2009-02-08T16:00:00+00:00
Tip
Because this is the only use of the compare method, it is reasonable to put this special knowledge that the sort is from big to small right into the compare method itself. The alternative is to sort small to big, and have the calling method reverse the results, as you saw in Example 12-1.
To test the length of the FileInfo object, you must cast the Object parameters to FileInfo objects (which is safe because you know this method will never receive anything else):
if (file1.Length > file2.Length) { return -1; } if (file1.Length < file2.Length) { return 1; } return 0; } }
Returning to GetFileList( ), you were about to instantiate the IComparer reference and pass it to the Sort( ) method of fileList:
IComparer<FileInfo> comparer = ( IComparer<FileInfo> ) new FileComparer(); fileList.Sort(comparer);
That done, you can return fileList to the calling method:
return fileList;
The calling method was btnCopy_Click. Remember, you went off to GetFileList( ) in the first line of the event handler!
protected void btnCopy_Click (object sender, System.EventArgs e) {List<FileInfo> fileList = GetFileList();
At this point, you've returned with a sorted list of File objects, each representing a file selected in the source TreeView.
You can now iterate through the list, copying the files and updating the UI:
foreach (FileInfo file in fileList) { try { lblStatus.Text = "Copying " + txtTargetDir.Text + "\\" + file.Name + "..."; Application.DoEvents( ); file.CopyTo(txtTargetDir.Text + "\\" + file.Name,chkOverwrite.Checked); } catch (Exception ex) { MessageBox.Show(ex.Message); } } lblStatus.Text = "Done.";
As you go, write the progress to the lblStatus label and call Application.DoEvents( ) to give the UI an opportunity to redraw. Then call CopyTo( ) on the file, passing in the target directory obtained from the text field, and a Boolean flag indicating whether the file should be overwritten if it already exists.
You'll notice that the flag you pass in is the value of the chkOverWrite checkbox. The Checked property evaluates true if the checkbox is checked and false if not.
The copy is wrapped in a try block because you can anticipate any number of things going wrong when copying files. For now, handle all exceptions by popping up a dialog box with the error; you might want to take corrective action in a commercial application.
That's it; you've implemented file copying!
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12571)
Hello! Python by Anthony Briggs(9916)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9779)
Dependency Injection in .NET by Mark Seemann(9340)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9254)
Hit Refresh by Satya Nadella(8819)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8300)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7781)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Kubernetes Operator Framework Book by Michael Dame(7635)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7425)
Practical Computer Architecture with Python and ARM by Alan Clements(7348)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7333)
Robo-Advisor with Python by Aki Ranin(7307)
Building Low Latency Applications with C++ by Sourav Ghosh(7216)
Svelte with Test-Driven Development by Daniel Irvine(7175)
