Search Results for

    Show / Hide Table of Contents

    FileOrDirectoryExists Constraint

    FileOrDirectoryExistsConstraint tests that a file or directory exists at the specified path.

    Usage

    Does.Exist
    Does.Not.Exist
    

    Modifiers

    .IgnoreDirectories   // Only check for file existence
    .IgnoreFiles         // Only check for directory existence
    

    Examples

    [Test]
    public void FileOrDirectoryExistsConstraint_Examples()
    {
        var tempFile = Path.GetTempFileName();
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(tempDir);
    
        try
        {
            // Test file or directory existence
            Assert.That(tempFile, Does.Exist);
            Assert.That(tempDir, Does.Exist);
            Assert.That("nonexistent.txt", Does.Not.Exist);
    
            // Test with FileInfo/DirectoryInfo
            Assert.That(new FileInfo(tempFile), Does.Exist);
            Assert.That(new DirectoryInfo(tempDir), Does.Exist);
        }
        finally
        {
            File.Delete(tempFile);
            Directory.Delete(tempDir);
        }
    }
    

    Notes

    1. The constraint accepts string paths, FileInfo, or DirectoryInfo objects.
    2. By default, the constraint passes if either a file or directory exists at the path.
    3. Use .IgnoreDirectories to require a file specifically.
    4. Use .IgnoreFiles to require a directory specifically.

    See Also

    • EmptyDirectory Constraint - Test if directory is empty
    • SamePath Constraint - Compare paths
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0