示例#1
0
        public override Stream OpenFileStream(string path, FileMode fileMode, FileAccess fileAccess, FileShare shareMode, FileOptions options, bool flushesToDisk)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            if (fileMode == FileMode.Create)
            {
                if (file != null)
                {
                    this.RootDirectory.RemoveFile(path);
                }

                return(this.CreateAndOpenFileStream(path));
            }

            if (fileMode == FileMode.OpenOrCreate)
            {
                if (file == null)
                {
                    return(this.CreateAndOpenFileStream(path));
                }
            }
            else
            {
                file.ShouldNotBeNull();
            }

            return(file.GetContentStream());
        }
示例#2
0
        public override byte[] ReadAllBytes(string path)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            using (Stream s = file.GetContentStream())
            {
                int count = (int)s.Length;

                int    pos    = 0;
                byte[] result = new byte[count];
                while (count > 0)
                {
                    int n = s.Read(result, pos, count);
                    if (n == 0)
                    {
                        throw new IOException("Unexpected end of stream");
                    }

                    pos   += n;
                    count -= n;
                }

                return(result);
            }
        }
示例#3
0
        public override string ReadAllText(string path)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            using (StreamReader reader = new StreamReader(file.GetContentStream()))
            {
                return(reader.ReadToEnd());
            }
        }
示例#4
0
        public override IEnumerable <string> ReadLines(string path)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            using (StreamReader reader = new StreamReader(file.GetContentStream()))
            {
                while (!reader.EndOfStream)
                {
                    yield return(reader.ReadLine());
                }
            }
        }
示例#5
0
        public override Stream OpenFileStream(string path, FileMode fileMode, FileAccess fileAccess, NativeMethods.FileAttributes attributes, FileShare shareMode)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            if (fileMode == FileMode.OpenOrCreate)
            {
                if (file == null)
                {
                    return(this.CreateAndOpenFileStream(path));
                }
            }
            else
            {
                file.ShouldNotBeNull();
            }

            return(file.GetContentStream());
        }