Exemplo n.º 1
0
        public static void Delete(string path)
        {
            Path.Validate(path);

            if (Environment.IsRunningOnWindows && path == ":")
            {
                throw new NotSupportedException("Only ':' In path");
            }

            SecurityManager.EnsureElevatedPermissions();              // this is a no-op outside moonlight

            MonoIOError error;
            bool        success;

            if (MonoIO.ExistsSymlink(path, out error))
            {
                /* RemoveDirectory maps to rmdir()
                 * which fails on symlinks (ENOTDIR)
                 */
                success = MonoIO.DeleteFile(path, out error);
            }
            else
            {
                success = MonoIO.RemoveDirectory(path, out error);
            }

            if (!success)
            {
                /*
                 * FIXME:
                 * In io-layer/io.c rmdir returns error_file_not_found if directory does not exist.
                 * So maybe this could be handled somewhere else?
                 */
                if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
                {
                    if (File.Exists(path))
                    {
                        throw new IOException("Directory does not exist, but a file of the same name exists.");
                    }
                    else
                    {
                        throw new DirectoryNotFoundException("Directory does not exist.");
                    }
                }
                else
                {
                    throw MonoIO.GetException(path, error);
                }
            }
        }
Exemplo n.º 2
0
        static void RecursiveDelete(string path)
        {
            MonoIOError error;

            foreach (string dir in GetDirectories(path))
            {
                if (MonoIO.ExistsSymlink(dir, out error))
                {
                    MonoIO.DeleteFile(dir, out error);
                }
                else
                {
                    RecursiveDelete(dir);
                }
            }

            foreach (string file in GetFiles(path))
            {
                File.Delete(file);
            }

            Directory.Delete(path);
        }
Exemplo n.º 3
0
        public static void Delete(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Length == 0)
            {
                throw new ArgumentException("Path is empty");
            }

            if (path.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("Path contains invalid chars");
            }

            if (path.Trim().Length == 0)
            {
                throw new ArgumentException("Only blank characters in path");
            }

            if (path == ":")
            {
                throw new NotSupportedException("Only ':' In path");
            }

            MonoIOError error;
            bool        success;

            if (MonoIO.ExistsSymlink(path, out error))
            {
                /* RemoveDirectory maps to rmdir()
                 * which fails on symlinks (ENOTDIR)
                 */
                success = MonoIO.DeleteFile(path, out error);
            }
            else
            {
                success = MonoIO.RemoveDirectory(path, out error);
            }

            if (!success)
            {
                /*
                 * FIXME:
                 * In io-layer/io.c rmdir returns error_file_not_found if directory does not exists.
                 * So maybe this could be handled somewhere else?
                 */
                if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
                {
                    if (File.Exists(path))
                    {
                        throw new IOException("Directory does not exist, but a file of the same name exist.");
                    }
                    else
                    {
                        throw new DirectoryNotFoundException("Directory does not exist.");
                    }
                }
                else
                {
                    throw MonoIO.GetException(path, error);
                }
            }
        }