2009-05-05 Miguel de Icaza <miguel@novell.com>
authorMiguel de Icaza <miguel@gnome.org>
Tue, 5 May 2009 22:58:16 +0000 (22:58 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Tue, 5 May 2009 22:58:16 +0000 (22:58 -0000)
* Contribution from David Uvalle <david.uvalle@gmail.com> that
implements FileInfo.Replace.

svn path=/trunk/mcs/; revision=133617

mcs/class/corlib/System.IO/ChangeLog
mcs/class/corlib/System.IO/FileInfo.cs

index 6562527596ee07e154f2179968dbcc083f9f73b1..4f4953d60bc799c4bca590679799fdcc3bca1d12 100644 (file)
@@ -1,3 +1,8 @@
+2009-05-05  Miguel de Icaza  <miguel@novell.com>
+
+       * Contribution from David Uvalle <david.uvalle@gmail.com> that
+       implements FileInfo.Replace.
+
 2009-04-25  Miguel de Icaza  <miguel@novell.com>
 
        * StreamReader.cs (DataAvailable): New internal function to work
index 18b88ae00655c7c971ef9d2887b878c1b8f51a44..6dc3fe9baf30e1623648008d7d082d845c601bd8 100644 (file)
@@ -286,7 +286,32 @@ namespace System.IO {
                public FileInfo Replace (string destinationFileName,
                                         string destinationBackupFileName)
                {
-                       throw new NotImplementedException ();
+                       string destinationFullPath = null;
+                       if (!Exists)
+                               throw new FileNotFoundException ();
+                       if (destinationFileName == null)
+                               throw new ArgumentNullException ("destinationFileName");
+                       if (destinationFileName.Length == 0)
+                               throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
+
+                       destinationFullPath = Path.GetFullPath (destinationFileName);
+
+                       if (!File.Exists (destinationFullPath))
+                               throw new FileNotFoundException ();
+
+                       FileAttributes attrs = File.GetAttributes (destinationFullPath);
+
+                       if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
+                                       throw new UnauthorizedAccessException (); 
+
+                       if (destinationBackupFileName != null) {
+                               if (destinationBackupFileName.Length == 0)
+                                       throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
+                               File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
+                       }
+                       File.Copy (FullPath, destinationFullPath,true);
+                       File.Delete (FullPath);
+                       return new FileInfo (destinationFullPath);
                }
                
                [ComVisible (false)]