How to get MaxPath per disk (volume) in Net5

  .net-5, c++, pinvoke, windows

I used to use PInvoke:

[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private extern static bool GetVolumeInformation(
            string rootPathName,
            StringBuilder volumeNameBuffer,
            int volumeNameSize,
            out uint volumeSerialNumber,
            out uint maximumComponentLength,
            out FileSystemFeature fileSystemFlags,
            StringBuilder fileSystemNameBuffer,
            int nFileSystemNameSize);

    public static VolumeInformation GetVolumeInfo(string path)
            {
                string root = System.IO.Path.GetPathRoot(path);
                const int maxPath = 261;
                StringBuilder volumeNameBuffer = new StringBuilder(maxPath);
                StringBuilder fileSystemNameBuffer = new StringBuilder(maxPath);
    
                VolumeInformation vi = new VolumeInformation();
    
                GetVolumeInformation(root,
                    volumeNameBuffer,
                    maxPath,
                    out vi._serialNumber,
                    out vi._maximumComponentLength,
                    out vi._fileSystemFlags,
                    fileSystemNameBuffer,
                    maxPath);
                
                vi.VolumeName = volumeNameBuffer.ToString();
                vi.FileSystemName = fileSystemNameBuffer.ToString();
    
                return vi;
            }

But it appears there is new way that replace PInvoke in .Net5: Calling Windows APIs in .NET5.

I can’t find how to get MaxPath? Any idea?
If you have a better and more general way without having to reference Windows, it would be even better.

Source: Windows Questions

LEAVE A COMMENT