public static void Delete(string path) { Path.Validate(path); if (Directory.Exists(path)) { throw new UnauthorizedAccessException(Locale.GetText("{0} is a directory", path)); } string DirName = Path.GetDirectoryName(path); if (DirName != String.Empty && !Directory.Exists(DirName)) { throw new DirectoryNotFoundException(Locale.GetText("Could not find a part of the path \"{0}\".", path)); } SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight MonoIOError error; if (!MonoIO.DeleteFile(path, out error)) { if (error != MonoIOError.ERROR_FILE_NOT_FOUND) { throw MonoIO.GetException(path, error); } } }
public static string GetDirectoryRoot(string path) { Path.Validate(path); SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight // FIXME nice hack but that does not work under windows return(new String(Path.DirectorySeparatorChar, 1)); }
public static void SetAttributes(string path, FileAttributes fileAttributes) { MonoIOError error; Path.Validate(path); if (!MonoIO.SetFileAttributes(path, fileAttributes, out error)) { throw MonoIO.GetException(path, error); } }
static internal IEnumerable <FileSystemInfo> EnumerateFileSystemInfos(string full, string searchPattern, SearchOption searchOption) { string path_with_pattern = Path.Combine(full, searchPattern); IntPtr handle = IntPtr.Zero; MonoIOError error; FileAttributes rattr; bool subdirs = searchOption == SearchOption.AllDirectories; Path.Validate(full); try { string s = MonoIO.FindFirst(full, path_with_pattern, out rattr, out error, out handle); if (s == null) { yield break; } if (error != 0) { throw MonoIO.GetException(Path.GetDirectoryName(path_with_pattern), (MonoIOError)error); } do { if (((rattr & FileAttributes.ReparsePoint) == 0)) { if ((rattr & FileAttributes.Directory) != 0) { yield return(new DirectoryInfo(s)); } else { yield return(new FileInfo(s)); } } if (((rattr & FileAttributes.Directory) != 0) && subdirs) { foreach (FileSystemInfo child in EnumerateFileSystemInfos(s, searchPattern, searchOption)) { yield return(child); } } } while ((s = MonoIO.FindNext(handle, out rattr, out error)) != null); } finally { if (handle != IntPtr.Zero) { MonoIO.FindClose(handle); } } }
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); } } }
public static void SetLastAccessTime(string path, DateTime lastAccessTime) { MonoIOError error; Path.Validate(path); if (!MonoIO.Exists(path, out error)) { throw MonoIO.GetException(path, error); } if (!MonoIO.SetLastAccessTime(path, lastAccessTime, out error)) { throw MonoIO.GetException(path, error); } }
public static void Delete(string path, bool recursive) { Path.Validate(path); SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight if (recursive) { RecursiveDelete(path); } else { Delete(path); } }
public static FileAttributes GetAttributes(string path) { Path.Validate(path); SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight MonoIOError error; FileAttributes attrs; attrs = MonoIO.GetFileAttributes(path, out error); if (error != MonoIOError.ERROR_SUCCESS) { throw MonoIO.GetException(path, error); } return(attrs); }
public static void AppendAllLines(string path, IEnumerable <string> contents) { Path.Validate(path); if (contents == null) { return; } using (TextWriter w = new StreamWriter(path, true)) { foreach (var line in contents) { w.Write(line); } } }
public static void WriteAllLines(string path, IEnumerable <string> contents, Encoding encoding) { Path.Validate(path); if (contents == null) { return; } using (TextWriter w = new StreamWriter(path, false, encoding)) { foreach (var line in contents) { w.WriteLine(line); } } }
// private // Does the common validation, searchPattern has already been checked for not-null static string ValidateDirectoryListing(string path, string searchPattern, out bool stop) { Path.Validate(path); string wild = Path.Combine(path, searchPattern); string wildpath = Path.GetDirectoryName(wild); if (wildpath.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("Pattern contains invalid characters", "pattern"); } MonoIOError error; if (!MonoIO.ExistsDirectory(wildpath, out error)) { if (error == MonoIOError.ERROR_SUCCESS) { MonoIOError file_error; if (MonoIO.ExistsFile(wildpath, out file_error)) { stop = true; return(wildpath); } } if (error != MonoIOError.ERROR_PATH_NOT_FOUND) { throw MonoIO.GetException(wildpath, error); } if (wildpath.IndexOfAny(SearchPattern.WildcardChars) == -1) { throw new DirectoryNotFoundException("Directory '" + wildpath + "' not found."); } if (path.IndexOfAny(SearchPattern.WildcardChars) == -1) { throw new ArgumentException("Pattern is invalid", "searchPattern"); } throw new ArgumentException("Path is invalid", "path"); } stop = false; return(wild); }
public static void SetAttributes(string path, FileAttributes fileAttributes) { #if MONO if (((uint)fileAttributes & 0x80000000) != 0) { MonoIOError error; Path.Validate(path); if (!MonoIO.SetFileAttributes(path, fileAttributes, out error)) { throw MonoIO.GetException(path, error); } return; } #endif string fullPath = Path.GetFullPath(path); FileSystem.SetAttributes(fullPath, fileAttributes); }
public static DirectoryInfo GetParent(string path) { Path.Validate(path); // return null if the path is the root directory if (IsRootDirectory(path)) { return(null); } string parent_name = Path.GetDirectoryName(path); if (parent_name.Length == 0) { parent_name = GetCurrentDirectory(); } return(new DirectoryInfo(parent_name)); }
static void EnumerateCheck(string path, string searchPattern, SearchOption searchOption) { if (searchPattern == null) { throw new ArgumentNullException("searchPattern"); } if (searchPattern.Length == 0) { return; } if (searchOption != SearchOption.TopDirectoryOnly && searchOption != SearchOption.AllDirectories) { throw new ArgumentOutOfRangeException("searchoption"); } Path.Validate(path); SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight }
public static DateTime GetLastWriteTime(string path) { MonoIOStat stat; MonoIOError error; Path.Validate(path); SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight if (!MonoIO.GetFileStat(path, out stat, out error)) { if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND) { return(DefaultLocalFileTime); } else { throw new IOException(path); } } return(DateTime.FromFileTime(stat.LastWriteTime)); }
static internal IEnumerable <FileSystemInfo> EnumerateFileSystemInfos(string basePath, string searchPattern, SearchOption searchOption) { Path.Validate(basePath); SafeFindHandle findHandle = null; try { string filePath; int nativeAttrs; string basePathWithPattern = Path.Combine(basePath, searchPattern); int nativeError; try {} finally { findHandle = new SafeFindHandle(MonoIO.FindFirstFile(basePathWithPattern, out filePath, out nativeAttrs, out nativeError)); } if (findHandle.IsInvalid) { MonoIOError error = (MonoIOError)nativeError; if (error != MonoIOError.ERROR_FILE_NOT_FOUND) { throw MonoIO.GetException(Path.GetDirectoryName(basePathWithPattern), error); } yield break; } do { if (filePath == null) { yield break; } if (filePath == "." || filePath == "..") { continue; } FileAttributes attrs = (FileAttributes)nativeAttrs; string fullPath = Path.Combine(basePath, filePath); if ((attrs & FileAttributes.ReparsePoint) == 0) { if ((attrs & FileAttributes.Directory) != 0) { yield return(new DirectoryInfo(fullPath)); } else { yield return(new FileInfo(fullPath)); } } if ((attrs & FileAttributes.Directory) != 0 && searchOption == SearchOption.AllDirectories) { foreach (FileSystemInfo child in EnumerateFileSystemInfos(fullPath, searchPattern, searchOption)) { yield return(child); } } } while (MonoIO.FindNextFile(findHandle.DangerousGetHandle(), out filePath, out nativeAttrs, out int _)); } finally { if (findHandle != null) { findHandle.Dispose(); } } }
public static IEnumerable <string> EnumerateFileSystemEntries(string path) { Path.Validate(path); // no need for EnumerateCheck since we supply valid arguments SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight return(EnumerateKind(path, "*", SearchOption.TopDirectoryOnly, FileAttributes.Normal | FileAttributes.Directory)); }