OpenFile() public method

public OpenFile ( string fileName ) : Stream
fileName string
return Stream
示例#1
0
        public void OpenFileReturnsWriteableStreamPositionedAtFileStart()
        {
            // Arrange
            PhysicalFileSystem fs = new PhysicalFileSystem(testRoot);
            WriteTestFile("testOpenWrite.txt", "Foo Bar Baz");

            // Act
            using (Stream strm = fs.OpenFile("testOpenWrite.txt"))
            {
                using (StreamWriter writer = new StreamWriter(strm))
                {
                    writer.Write("Quz");
                }
            }

            // Assert
            Assert.Equal("Quz Bar Baz", File.ReadAllText(ResolveTestFile("testOpenWrite.txt")));
        }
示例#2
0
        public void OpenFileLocksFileForAllAccess()
        {
            // Arrange
            PhysicalFileSystem fs = new PhysicalFileSystem(testRoot);
            WriteTestFile("testLock.txt", "Foo Bar Baz");

            // Act
            using (Stream strm = fs.OpenFile("testLock.txt"))
            {
                // Assert
                IOException ex = Assert.Throws<IOException>(() => fs.OpenFile("testLock.txt"));
                Assert.Equal(String.Format("The process cannot access the file '{0}' because it is being used by another process.", ResolveTestFile("testLock.txt")), ex.Message);
            }
        }
示例#3
0
        public void OpenFileReturnsStreamContainingAllFileData()
        {
            // Arrange
            PhysicalFileSystem fs = new PhysicalFileSystem(testRoot);
            WriteTestFile("testOpenRead.txt", "Foo Bar Baz");

            // Act
            Stream strm = fs.OpenFile("testOpenRead.txt");

            // Assert
            Assert.Equal("Foo Bar Baz", GetContent(strm));
        }
示例#4
0
        public void OpenFileCreatesParentDirectoryTreeIfNecessary()
        {
            // Arrange
            PhysicalFileSystem fs = new PhysicalFileSystem(testRoot);

            // Act
            using (Stream strm = fs.OpenFile(@"testParent\folder\testOpenWrite.txt")) { }

            // Assert
            Assert.True(Directory.Exists(ResolveTestFile(@"testParent\folder")));
        }