Esempio n. 1
0
        /// <summary>
        /// Opens the file for appending. If the file does not exist, this method will create the file and then open it for appending.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns>A Stream for appending text to the file.</returns>
        public static Stream AppendText(AFile file)
        {
            Exceptions.NotNullException<AFile>(file, nameof(file));

            return file.Open(FileAccess.Write, FileMode.Append);
        }
Esempio n. 2
0
        /// <summary>
        /// Opens the specified file with the file access.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="fileAccess">The file access.</param>
        /// <param name="fileMode">The file mode.</param>
        /// <returns>The stream representing the opened file.</returns>
        public static Stream Open(AFile file, FileAccess fileAccess = FileAccess.ReadWrite, FileMode fileMode = FileMode.OpenOrCreate)
        {
            Exceptions.NotNullException<AFile>(file, nameof(file));

            return file.Open(fileAccess, fileMode);
        }
Esempio n. 3
0
        /// <summary>
        /// Reads the entire file and returns the contents as a byte array.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns>A byte array representing the contents of the file.</returns>
        public static byte[] ReadAllBytes(AFile file)
        {
            Exceptions.NotNullException<AFile>(file, nameof(file));

            var output = new byte[0];

            if (!file.IsOpen)
                file.Open(FileAccess.Read, FileMode.Open);

            long length = file.Stream.Length;
            long position = 0;
            output = new byte[length];
            var tmp = new byte[0];
            while (length > 0)
            {
                var tmpLength = (int)(Int32.MaxValue - length);
                if (tmpLength < 0)
                    tmpLength = (int)length;
                tmp = new byte[tmpLength];

                file.Stream.Read(tmp, 0, tmpLength);
                for (int i = 0; i < tmpLength; i++)
                    output[position++] = tmp[i];

                length -= tmpLength;
            }

            return output;
        }