12.5 Copying and Moving a File

 <  Day Day Up  >  

You want to copy or move a file from one location on the local file system to a different location.


Technique

The FileInfo and File classes both contain methods to manipulate files without having to perform any type of I/O methods on them beforehand. This section looks at the File methods, but the methods work with the FileInfo class as well. The previous section discusses some of the differences between the two.

To copy a file to a different location, call the static Copy method. This method accepts two strings denoting the full path of the source and the destination to copy the file to. Note that the filenames themselves don't have to be the same. If the filenames are the same, an optional Boolean value tells the method what to do. If you set this value to true , the destination file is overwritten. Setting the value to false causes the method to throw an exception if the final destination file already exists. If no value is specified, then the method throws an exception if the file exists. In other words, the default overwrite method is set to false :

 
 private void mnuSaveAs_Click(object sender, System.EventArgs e) {     if( saveFileDialog1.ShowDialog(this) == DialogResult.OK )     {         // back up the file first by copying it a new file         File.Copy( curFilePath, "bkup.bak", true );         // save to new file         SaveText( tbView.Text, saveFileDialog1.FileName );         // update new filename         curFilePath = saveFileDialog1.FileName;         // reset dirty flag         bDirty = false;         // update window title to remove dirty indicator         this.Text = "Simple Pad - " + curFilePath;     } } 

Moving a file utilizes the same methodology. The method to move a file is called Move , and it accepts the same type of parameters as the Copy method. One thing that you must ensure is that you don't have a stream open on the source file. If the file is open and inaccessible to outside processes, an exception is thrown and the method fails. One notable difference between Move and Copy , other than the fact that the source file is removed when calling Move , is the absence of the overwrite flag. Because the method is designed to move the file from one location to another, the destination file cannot exist. The workaround is to call the File.Exists method on the destination file, and if it returns true , call File.DeleteFile before moving:

 
 try {     if( File.Exists( destPath ) )         File.Delete( destPath );     File.Move( srcPath, destPath ); } catch( Exception e ) {     MessageBox( e.Message ); } 

Comments

Moving and copying files from one location to another is pretty straightforward when using the File or FileInfo class. One thing that you have to be wary of, however, is the time it takes to move or copy a file. Both of these methods are synchronous, which means they do not return until the operation finishes. If the file you are copying or moving is large, then your application appears as if it has hung. To combat this issue, you have to use the threading techniques discussed in Chapter 22, "Threading and Synchronization," to keep the Windows messages flowing on your main UI thread.

Additionally, there is no default user interface showing the progress of the file-copy operation. Whenever you copy or move a file within Windows Explorer, you'll notice a small window with an animation denoting what Explorer is currently doing. For large files, this window stays up for a considerably longer amount of time ”as compared to smaller files in which you see just a quick flash as the window quickly opens and closes . You can, however, use one of the shell functions in shell32.dll named SHFileOperation to perform a copy or move and display the animation window. To do so, you utilize something called PInvoke , which is discussed in Chapter 21, "Securing Your Code."

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net