*/
success = MonoIO.DeleteFile (path, out error);
} else {
- success = MonoIO.RemoveDirectory (path,
- out error);
+ success = MonoIO.RemoveDirectory (path, out error);
}
if (!success) {
{
return File.GetLastAccessTime (path);
}
-
+
public static DateTime GetLastAccessTimeUtc (string path)
{
return GetLastAccessTime (path).ToUniversalTime ();
}
-
+
public static DateTime GetLastWriteTime (string path)
{
return File.GetLastWriteTime (path);
static bool IsRootDirectory (string path)
{
// Unix
- if (Path.DirectorySeparatorChar == '/' && path == "/")
- return true;
+ if (Path.DirectorySeparatorChar == '/' && path == "/")
+ return true;
- // Windows
- if (Path.DirectorySeparatorChar == '\\')
- if (path.Length == 3 && path.EndsWith (":\\"))
- return true;
+ // Windows
+ if (Path.DirectorySeparatorChar == '\\')
+ if (path.Length == 3 && path.EndsWith (":\\"))
+ return true;
- return false;
+ return false;
}
public static DirectoryInfo GetParent (string path)
return new DirectoryInfo (parent_name);
}
- public static void Move (string src, string dest)
+ public static void Move (string sourceDirName, string destDirName)
{
- if (src == null)
- throw new ArgumentNullException ("src");
+ if (sourceDirName == null)
+ throw new ArgumentNullException ("sourceDirName");
- if (dest == null)
- throw new ArgumentNullException ("dest");
+ if (destDirName == null)
+ throw new ArgumentNullException ("destDirName");
- if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
- throw new ArgumentException ("Invalid source directory name: " + src, "src");
+ if (sourceDirName.Trim () == "" || sourceDirName.IndexOfAny (Path.InvalidPathChars) != -1)
+ throw new ArgumentException ("Invalid source directory name: " + sourceDirName, "sourceDirName");
- if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
- throw new ArgumentException ("Invalid target directory name: " + dest, "dest");
+ if (destDirName.Trim () == "" || destDirName.IndexOfAny (Path.InvalidPathChars) != -1)
+ throw new ArgumentException ("Invalid target directory name: " + destDirName, "destDirName");
- if (src == dest)
- throw new IOException ("Source directory cannot be same as a target directory.");
+ if (sourceDirName == destDirName)
+ throw new IOException ("Source and destination path must be different.");
- if (Exists (dest))
- throw new IOException (dest + " already exists.");
+ if (Exists (destDirName))
+ throw new IOException (destDirName + " already exists.");
- if (!Exists (src))
- throw new DirectoryNotFoundException (src + " does not exist");
+ if (!Exists (sourceDirName) && !File.Exists (sourceDirName))
+ throw new DirectoryNotFoundException (sourceDirName + " does not exist");
MonoIOError error;
- if (!MonoIO.MoveFile (src, dest, out error))
+ if (!MonoIO.MoveFile (sourceDirName, destDirName, out error))
throw MonoIO.GetException (error);
}
#endif
}
}
-
[TestFixture]\r
public class DirectoryTest\r
{\r
- CultureInfo old_culture;\r
string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");\r
static readonly char DSC = Path.DirectorySeparatorChar;\r
\r
{\r
if (!Directory.Exists (TempFolder))\r
Directory.CreateDirectory (TempFolder);\r
- old_culture = Thread.CurrentThread.CurrentCulture;\r
- Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);\r
+\r
+ Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");\r
}\r
\r
[TearDown]\r
public void TearDown () {\r
if (Directory.Exists (TempFolder))\r
Directory.Delete (TempFolder, true);\r
- Thread.CurrentThread.CurrentCulture = old_culture;\r
}\r
\r
[Test]\r
}\r
\r
[Test]\r
- public void Move ()\r
+ public void Move_DestDirName_Empty ()\r
+ {\r
+ try {\r
+ Directory.Move (TempFolder, string.Empty);\r
+ Assert.Fail ("#A1");\r
+ } catch (ArgumentException ex) {\r
+ // Empty file name is not legal\r
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
+ Assert.IsNull (ex.InnerException, "#A3");\r
+ Assert.IsNotNull (ex.Message, "#A4");\r
+ Assert.IsNotNull (ex.ParamName, "#A5");\r
+ Assert.AreEqual ("destDirName", ex.ParamName, "#A6");\r
+ }\r
+\r
+ try {\r
+ Directory.Move (TempFolder, " ");\r
+ Assert.Fail ("#B1");\r
+ } catch (ArgumentException ex) {\r
+ // The path is not of a legal form\r
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
+ Assert.IsNull (ex.InnerException, "#B3");\r
+ Assert.IsNotNull (ex.Message, "#B4");\r
+ }\r
+ }\r
+\r
+ [Test]\r
+ public void Move_DestDirName_Null ()\r
+ {\r
+ try {\r
+ Directory.Move (TempFolder, (string) null);\r
+ Assert.Fail ("#1");\r
+ } catch (ArgumentNullException ex) {\r
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
+ Assert.IsNull (ex.InnerException, "#3");\r
+ Assert.IsNotNull (ex.Message, "#4");\r
+ Assert.IsNotNull (ex.ParamName, "#5");\r
+ Assert.AreEqual ("destDirName", ex.ParamName, "#6");\r
+ }\r
+ }\r
+\r
+ [Test]\r
+ public void Move_SourceDirName_Empty ()\r
+ {\r
+ try {\r
+ Directory.Move (string.Empty, TempFolder);\r
+ Assert.Fail ("#A1");\r
+ } catch (ArgumentException ex) {\r
+ // Empty file name is not legal\r
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
+ Assert.IsNull (ex.InnerException, "#A3");\r
+ Assert.IsNotNull (ex.Message, "#A4");\r
+ Assert.IsNotNull (ex.ParamName, "#A5");\r
+ Assert.AreEqual ("sourceDirName", ex.ParamName, "#A6");\r
+ }\r
+\r
+ try {\r
+ Directory.Move (" ", TempFolder);\r
+ Assert.Fail ("#B1");\r
+ } catch (ArgumentException ex) {\r
+ // The path is not of a legal form\r
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
+ Assert.IsNull (ex.InnerException, "#B3");\r
+ Assert.IsNotNull (ex.Message, "#B4");\r
+ }\r
+ }\r
+\r
+ [Test]\r
+ public void Move_SourceDirName_Null ()\r
+ {\r
+ try {\r
+ Directory.Move ((string) null, TempFolder);\r
+ Assert.Fail ("#1");\r
+ } catch (ArgumentNullException ex) {\r
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
+ Assert.IsNull (ex.InnerException, "#3");\r
+ Assert.IsNotNull (ex.Message, "#4");\r
+ Assert.IsNotNull (ex.ParamName, "#5");\r
+ Assert.AreEqual ("sourceDirName", ex.ParamName, "#6");\r
+ }\r
+ }\r
+\r
+ [Test]\r
+ public void MoveDirectory ()\r
{\r
string path = TempFolder + DSC + "DirectoryTest.Test.9";\r
string path2 = TempFolder + DSC + "DirectoryTest.Test.10";\r
Directory.Delete (path2 + DSC + "dir", true);\r
}\r
}\r
- \r
+\r
[Test]\r
- [ExpectedException(typeof(IOException))]\r
- public void MoveException1 ()\r
+ [ExpectedException (typeof (IOException))]\r
+ public void MoveDirectory_Same ()\r
{\r
string path = TempFolder + DSC + "DirectoryTest.Test.8";\r
DeleteDirectory (path);\r
}\r
\r
[Test]\r
- [ExpectedException(typeof(ArgumentException))]\r
- public void MoveException2 ()\r
+ public void MoveFile ()\r
{\r
- string path = TempFolder + DSC + "DirectoryTest.Test.11";\r
- DeleteDirectory (path);\r
+ string tempFile1 = Path.Combine (TempFolder, "temp1.txt");\r
+ string tempFile2 = Path.Combine (TempFolder, "temp2.txt");\r
+\r
+ using (StreamWriter sw = File.CreateText (tempFile1)) {\r
+ sw.Write ("temp1");\r
+ }\r
+ Assert.IsFalse (File.Exists (tempFile2), "#1");\r
+ Directory.Move (tempFile1, tempFile2);\r
+ Assert.IsFalse (File.Exists (tempFile1), "#2");\r
+ Assert.IsTrue (File.Exists (tempFile2), "#3");\r
+ using (StreamReader sr = File.OpenText (tempFile2)) {\r
+ Assert.AreEqual ("temp1", sr.ReadToEnd (), "#4");\r
+ }\r
+ }\r
+\r
+ [Test]\r
+ public void MoveFile_DestDir_Exists ()\r
+ {\r
+ string tempFile = Path.Combine (TempFolder, "temp1.txt");\r
+ string tempDir = Path.Combine (TempFolder, "temp2");\r
+\r
+ using (StreamWriter sw = File.CreateText (tempFile)) {\r
+ sw.Write ("temp1");\r
+ }\r
+ Directory.CreateDirectory (tempDir);\r
+\r
try {\r
- Directory.Move ("", path);\r
- } finally {\r
- DeleteDirectory (path);\r
+ Directory.Move (tempFile, tempDir);\r
+ Assert.Fail ("#A1");\r
+ } catch (IOException ex) {\r
+ // Cannot create a file when that file already exists\r
+ Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");\r
+ Assert.IsNull (ex.InnerException, "#A3");\r
+ Assert.IsNotNull (ex.Message, "#A4");\r
}\r
+\r
+ Assert.IsTrue (File.Exists (tempFile), "#B1");\r
+ Assert.IsFalse (File.Exists (tempDir), "#B2");\r
+ Assert.IsTrue (Directory.Exists (tempDir), "#B3");\r
}\r
\r
[Test]\r
- [ExpectedException(typeof(ArgumentException))]\r
- public void MoveException3 ()\r
+ public void MoveFile_DestFile_Exists ()\r
{\r
- string path = TempFolder + DSC + "DirectoryTest.Test.12";\r
- DeleteDirectory (path);\r
+ string tempFile1 = Path.Combine (TempFolder, "temp1.txt");\r
+ string tempFile2 = Path.Combine (TempFolder, "temp2.txt");\r
+\r
+ using (StreamWriter sw = File.CreateText (tempFile1)) {\r
+ sw.Write ("temp1");\r
+ }\r
+ using (StreamWriter sw = File.CreateText (tempFile2)) {\r
+ sw.Write ("temp2");\r
+ }\r
+\r
try {\r
- Directory.Move (" ", path);\r
- } finally {\r
- DeleteDirectory (path);\r
+ Directory.Move (tempFile1, tempFile2);\r
+ Assert.Fail ("#A1");\r
+ } catch (IOException ex) {\r
+ // Cannot create a file when that file already exists\r
+ Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");\r
+ Assert.IsNull (ex.InnerException, "#A3");\r
+ Assert.IsNotNull (ex.Message, "#A4");\r
+ }\r
+\r
+ Assert.IsTrue (File.Exists (tempFile1), "#B1");\r
+ using (StreamReader sr = File.OpenText (tempFile1)) {\r
+ Assert.AreEqual ("temp1", sr.ReadToEnd (), "#B2");\r
+ }\r
+\r
+ Assert.IsTrue (File.Exists (tempFile2), "#C1");\r
+ using (StreamReader sr = File.OpenText (tempFile2)) {\r
+ Assert.AreEqual ("temp2", sr.ReadToEnd (), "#C2");\r
+ }\r
+ }\r
+\r
+ [Test]\r
+ public void MoveFile_Same ()\r
+ {\r
+ string tempFile = Path.Combine (TempFolder, "temp.txt");\r
+\r
+ try {\r
+ Directory.Move (tempFile, tempFile);\r
+ Assert.Fail ("#1");\r
+ } catch (IOException ex) {\r
+ // Source and destination path must be different\r
+ Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");\r
+ Assert.IsNull (ex.InnerException, "#3");\r
+ Assert.IsNotNull (ex.Message, "#4");\r
}\r
}\r
\r
\r
[Test]\r
[ExpectedException(typeof(IOException))]\r
- public void MoveException6 ()\r
+ public void MoveDirectory_Dest_SubDir ()\r
{\r
string path = TempFolder + DSC + "DirectoryTest.Test.15";\r
DeleteDirectory (path);\r
}\r
\r
[Test]\r
- [ExpectedException(typeof(IOException))]\r
- public void MoveException7 ()\r
+ [ExpectedException (typeof (IOException))]\r
+ public void MoveDirectory_Dest_Exists ()\r
{\r
string path = TempFolder + DSC + "DirectoryTest.Test.16";\r
string path2 = TempFolder + DSC + "DirectoryTest.Test.17";\r