// \r
-// System.IO.File.cs \r
+// System.IO.FIle.cs \r
//\r
// \r
// Authors:\r
/// <summary>\r
/// \r
/// </summary>\r
- public sealed class File : Object\r
+ public sealed class File\r
{\r
private File () {}
+ \r
+ \r
public static StreamWriter AppendText (string path)\r
{ \r
return new StreamWriter (path, true);\r
}\r
- \r
+\r
+ [MonoTODO("Security Permision Checks")]\r
public static void Copy (string sourceFilename, string destFilename)\r
{\r
Copy (sourceFilename, destFilename, false);\r
}\r
- \r
+\r
public static void Copy (string src, string dest, bool overwrite)\r
{ \r
- if (src == null || dest == null)\r
- throw new ArgumentNullException ();\r
- if (src == "" || dest == "" ||\r
- src.IndexOfAny (Path.InvalidPathChars) != -1 ||\r
- dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
- throw new ArgumentException ();\r
+ if (src == null)\r
+ throw new ArgumentNullException ("src");\r
+ if (dest == null)\r
+ throw new ArgumentNullException ("dest");\r
+ if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)\r
+ throw new ArgumentException ("src");\r
+ if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
+ throw new ArgumentException ("dest");\r
+ if (src.IndexOf(':') > 1)\r
+ throw new NotSupportedException("src");\r
+ if (dest.IndexOf(':') > 1)\r
+ throw new NotSupportedException("dest");\r
+ if (!Exists (src)) {\r
+ throw new FileNotFoundException (src + " does not exist");\r
+ }\r
+ else {\r
+ if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){\r
+ throw new ArgumentException(src + " is a directory"); \r
+ }\r
+ }\r
+ if (Exists (dest)) {\r
+ if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){\r
+ throw new ArgumentException(dest + " is a directory"); \r
+ }\r
+ if (!overwrite)\r
+ throw new IOException (dest + " already exists");\r
+ }\r
+\r
+ string DirName = Path.GetDirectoryName(dest);\r
+ if (!Directory.Exists (DirName))\r
+ throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
\r
if (!MonoIO.CopyFile (src, dest, overwrite))\r
throw MonoIO.GetException ();\r
\r
public static FileStream Create (string path, int buffersize)\r
{\r
+ if (null == path)\r
+ throw new ArgumentNullException("path");\r
+ if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
+ throw new ArgumentException("path");\r
+ if (path.IndexOf(':') > 1)\r
+ throw new NotSupportedException();\r
+\r
+ string DirName = Path.GetDirectoryName(path);\r
+ if (!Directory.Exists (DirName))\r
+ throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
+ if (Exists(path)){\r
+ if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){\r
+ throw new UnauthorizedAccessException(path + " is a read-only"); \r
+ }\r
+ }\r
+\r
return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,\r
FileShare.None, buffersize);\r
}\r
\r
public static void Delete (string path)\r
{\r
- if (path == null)\r
- throw new ArgumentNullException ();\r
- if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)\r
- throw new ArgumentException ();\r
+ if (null == path)\r
+ throw new ArgumentNullException("path");\r
+ if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
+ throw new ArgumentException("path");\r
+ if (path.IndexOf(':') > 1)\r
+ throw new NotSupportedException();\r
+ if (Directory.Exists (path))\r
+ throw new UnauthorizedAccessException("path is a directory");\r
+\r
+ string DirName = Path.GetDirectoryName(path);\r
+ if (!Directory.Exists (DirName))\r
+ throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
+\r
if (!MonoIO.DeleteFile (path)){\r
Exception e = MonoIO.GetException ();\r
- if (e != null && !(e is FileNotFoundException))\r
+ if (! (e is FileNotFoundException))\r
throw e;\r
}\r
}\r
\r
public static bool Exists (string path)\r
{\r
+ // For security reasons no exceptions are thrown, only false is returned if there\r
+ // is any problem with the path or permissions. Minimizes what information can be\r
+ // discovered by using this method.\r
+ if (null == path || String.Empty == path.Trim() \r
+ || path.IndexOfAny(Path.InvalidPathChars) >= 0\r
+ || path.IndexOf(':') > 1)\r
+ return false;\r
+\r
return MonoIO.ExistsFile (path);\r
}\r
\r
public static FileAttributes GetAttributes (string path)\r
{\r
+ if (null == path)\r
+ throw new ArgumentNullException("path");\r
+ if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
+ throw new ArgumentException("path");\r
+ if (path.IndexOf(':') > 1)\r
+ throw new NotSupportedException();\r
+\r
+ string DirName = Path.GetDirectoryName(path);\r
+ if (!Directory.Exists(DirName))\r
+ throw new DirectoryNotFoundException("Directory '" + DirName + "' not found in '" + Environment.CurrentDirectory + "'.");\r
+\r
return MonoIO.GetFileAttributes (path);\r
}\r
\r
\r
public static void Move (string src, string dest)\r
{\r
- if (src == null || dest == null)\r
- throw new ArgumentNullException ();\r
- if (src == "" || dest == "" ||\r
- src.IndexOfAny (Path.InvalidPathChars) != -1 ||\r
- dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
- throw new ArgumentException ();\r
+ if (src == null)\r
+ throw new ArgumentNullException ("src");\r
+ if (dest == null)\r
+ throw new ArgumentNullException ("dest");\r
+ if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)\r
+ throw new ArgumentException ("src");\r
+ if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
+ throw new ArgumentException ("dest");\r
+ if (src.IndexOf(':') > 1)\r
+ throw new NotSupportedException("src");\r
+ if (dest.IndexOf(':') > 1)\r
+ throw new NotSupportedException("dest");\r
+ if (!Exists (src))\r
+ throw new FileNotFoundException (src + " does not exist");\r
+ if (Exists (dest) && ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory))\r
+ throw new ArgumentException(dest + " is a directory"); \r
+\r
+ string DirName;\r
+ DirName = Path.GetDirectoryName(src);\r
+ if (!Directory.Exists (DirName))\r
+ throw new DirectoryNotFoundException("Source directory not found: " + DirName);\r
+ DirName = Path.GetDirectoryName(dest);\r
+ if (!Directory.Exists (DirName))\r
+ throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
\r
if (!MonoIO.MoveFile (src, dest))\r
throw MonoIO.GetException ();\r