internal FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool anonymous, FileOptions options) { if (path == null) { throw new ArgumentNullException("path"); } if (path.Length == 0) { throw new ArgumentException("Path is empty"); } this.anonymous = anonymous; // ignore the Inheritable flag share &= ~FileShare.Inheritable; if (bufferSize <= 0) { throw new ArgumentOutOfRangeException("bufferSize", "Positive number required."); } if (mode < FileMode.CreateNew || mode > FileMode.Append) { #if MOBILE if (anonymous) { throw new ArgumentException("mode", "Enum value was out of legal range."); } else #endif throw new ArgumentOutOfRangeException("mode", "Enum value was out of legal range."); } if (access < FileAccess.Read || access > FileAccess.ReadWrite) { throw new ArgumentOutOfRangeException("access", "Enum value was out of legal range."); } if (share < FileShare.None || share > (FileShare.ReadWrite | FileShare.Delete)) { throw new ArgumentOutOfRangeException("share", "Enum value was out of legal range."); } if (path.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("Name has invalid chars"); } path = Path.InsecureGetFullPath(path); if (Directory.Exists(path)) { // don't leak the path information for isolated storage string msg = Locale.GetText("Access to the path '{0}' is denied."); throw new UnauthorizedAccessException(String.Format(msg, GetSecureFileName(path, false))); } /* Append streams can't be read (see FileMode * docs) */ if (mode == FileMode.Append && (access & FileAccess.Read) == FileAccess.Read) { throw new ArgumentException("Append access can be requested only in write-only mode."); } if ((access & FileAccess.Write) == 0 && (mode != FileMode.Open && mode != FileMode.OpenOrCreate)) { string msg = Locale.GetText("Combining FileMode: {0} with " + "FileAccess: {1} is invalid."); throw new ArgumentException(string.Format(msg, access, mode)); } SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight string dname = Path.GetDirectoryName(path); if (dname.Length > 0) { string fp = Path.GetFullPath(dname); if (!Directory.Exists(fp)) { // don't leak the path information for isolated storage string msg = Locale.GetText("Could not find a part of the path \"{0}\"."); string fname = (anonymous) ? dname : Path.GetFullPath(path); throw new DirectoryNotFoundException(String.Format(msg, fname)); } } // IsolatedStorage needs to keep the Name property to the default "[Unknown]" if (!anonymous) { this.name = path; } // TODO: demand permissions MonoIOError error; var nativeHandle = MonoIO.Open(path, mode, access, share, options, out error); if (nativeHandle == MonoIO.InvalidHandle) { // don't leak the path information for isolated storage throw MonoIO.GetException(GetSecureFileName(path), error); } this.safeHandle = new SafeFileHandle(nativeHandle, false); this.access = access; this.owner = true; /* Can we open non-files by name? */ if (MonoIO.GetFileType(safeHandle, out error) == MonoFileType.Disk) { this.canseek = true; this.async = (options & FileOptions.Asynchronous) != 0; } else { this.canseek = false; this.async = false; } if (access == FileAccess.Read && canseek && (bufferSize == DefaultBufferSize)) { /* Avoid allocating a large buffer for small files */ long len = Length; if (bufferSize > len) { bufferSize = (int)(len < 1000 ? 1000 : len); } } InitBuffer(bufferSize, false); if (mode == FileMode.Append) { this.Seek(0, SeekOrigin.End); this.append_startpos = this.Position; } else { this.append_startpos = 0; } }
/// <summary>Returns the absolute path for the specified path string.</summary> /// <returns>A string containing the fully qualified location of <paramref name="path" />, such as "C:\MyFile.txt".</returns> /// <param name="path">The file or directory for which to obtain absolute path information. </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="path" /> is a zero-length string, contains only white space, or contains one or more of the invalid characters defined in <see cref="M:System.IO.Path.GetInvalidPathChars" />.-or- The system could not retrieve the absolute path. </exception> /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permissions. </exception> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="path" /> is null. </exception> /// <exception cref="T:System.NotSupportedException"> /// <paramref name="path" /> contains a colon (":") that is not part of a volume identifier (for example, "c:\"). </exception> /// <exception cref="T:System.IO.PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. </exception> /// <filterpriority>1</filterpriority> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" PathDiscovery="*AllFiles*" /> /// </PermissionSet> public static string GetFullPath(string path) { return(Path.InsecureGetFullPath(path)); }