Skip to content

SetCreationTime, SetLastAccessTime, SetLastWriteTime Should not open a new stream to obtain a SafeFileHandle #20234

@TrabacchinLuigi

Description

@TrabacchinLuigi

API Proposal

namespace System.IO
{
    public static class FileHandleExtensions
    {
        public static DateTime GetLastWriteTimeUtc(this SafeFileHandle fileHandle);
        public static void SetLastWriteTimeUtc(this SafeFileHandle fileHandle, DateTime time);
        public static DateTime GetLastAccessTimeUtc(this SafeFileHandle fileHandle);
        public static void SetLastAccessTimeUtc(this SafeFileHandle fileHandle, DateTime time);
        public static DateTime GetCreationTimeUtc(this SafeFileHandle fileHandle);
        public static void SetCreationTimeUtc(this SafeFileHandle fileHandle, DateTime time);
    }
}

Original post

I know that in many cases it is ok to do so, but we found ourself in the case where we need to set file times without closing the actual FileStream, and this is possible via Windows API.
To do so we had to write unsafe code which mimic the functionality of the existing functions.
(which include rewriting System.IO.__Error.WinIOError(), this is obviusly a bad thing, because we have to maintain code wich already exist)
This could be extremely more usable and maintainable if the framework exposed those methods as SafeFileHandle Method/ExtensionMethod.

My proposal is to leave those functions unaltered but move the actual implementation in an ipotetic function:
SafeFileHandle.SetFileTimesUtc(DateTime? creationTimeUtc, DateTime? lastAccessTimeUtc, DateTime? lastWriteTimeUtc) which should be public.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-System.IOhelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions