Skip to content

FileSystemEventArgs.FullPath does not return a fully qualified path #62606

@UnityAlex

Description

@UnityAlex

Description

As per the documentation https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemeventargs.fullpath?view=net-6.0 the FullPath property should return a fully qualified path. However it appears the constructor for FileSystemEventArgs assumes the directory passed in is fully qualified: https://github.com/dotnet/runtime/blob/main/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemEventArgs.cs#L22

When used in conjunction with FileSystemWatcher it is possible for the path to be relative: https://github.com/dotnet/runtime/blob/main/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs#L260

Reproduction Steps

Repro code:

using System;
using System.IO;

namespace MyNamespace
{
    class Program
    {
        static void Main()
        {
            using var watcher = new FileSystemWatcher(@"bar");

            watcher.NotifyFilter = NotifyFilters.LastWrite;

            watcher.Changed += OnChanged;

            watcher.Filter = "*.txt";
            watcher.IncludeSubdirectories = false;
            watcher.EnableRaisingEvents = true;

            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }

        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }
            Console.WriteLine($"Changed: {e.FullPath}");
        }
    }
}
  1. I created the following dir structure on my machine:
    C:\foo\bar\footest.txt

  2. Set the working directory to be C:\foo

  3. Run the program and then modify footest.txt to trigger a changed event.

Expected behavior

Output should be:
Changed: C:\foo\bar\footest.txt

Actual behavior

Current output:
Changed: bar\footest.txt

Regression?

No response

Known Workarounds

Using Path.GetFullPath to fetch the fully qualified path

Console.WriteLine($"Changed: {Path.GetFullPath(e.FullPath)}");

Configuration

.Net 6.0
Windows 10 x64
After looking at the source I believe that this is not unique to Windows

Other information

No response

Metadata

Metadata

Assignees

Labels

area-System.IObreaking-changeIssue or PR that represents a breaking API or functional change over a prerelease.help wanted[up-for-grabs] Good issue for external contributors

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions