0

I have the following case, where I get an error using the .First() in ManagementObject variable, however, I can't figure out how to correct it. Can anyone help me? (using System.Management reference in project)

        public static long GetFileSizeOnDisk(string file)
    {
        FileInfo info = new FileInfo(file);
        uint clusterSize;

        var searcher = new ManagementObjectSearcher("select BlockSize,NumberOfBlocks from Win32_Volume WHERE DriveLetter = '" + info.Directory.Root.FullName.TrimEnd('\\') + "'")
            
        clusterSize = (uint)((ManagementObject)searcher.Get().First())["BlockSize"];

        uint hosize;
        uint losize = GetCompressedFileSizeW(file, out hosize);
        long size = (long) hosize << 32 | losize;
    
        return ((size + clusterSize - 1) / clusterSize) * clusterSize;
    }

Having also

        [DllImport("kernel32.dll")]
    static extern uint GetCompressedFileSizeW(
       [In, MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
       [Out, MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);

The error is:

Error   CS1061  'ManagementObjectCollection' does not contain a definition for 'First' and no accessible extension method 'First' accepting a first argument of type 'ManagementObjectCollection' could be found (are you missing a using directive or an assembly reference?)
Nick
  • 483
  • 1
  • 6
  • 15
  • 1
    The following may be helpful: https://stackoverflow.com/questions/71362463/how-to-get-the-ram-name/71388371#71388371 (see "GetMemory"). – Tu deschizi eu inchid Sep 06 '22 at 14:49
  • whats in the error? try to run the app As Admin just in case.. – Boppity Bop Sep 06 '22 at 20:02
  • @user9938 Thank you for the advice, I have looked into this and looks very promising; I tried the same (not for RAM but for 'BclockSize' although I can see that the property is correct. The error I get is: System.Management.ManagementException: 'Not found ' – Nick Sep 07 '22 at 05:42
  • @boppityBop I updated the question with the error thrown, thank you. – Nick Sep 07 '22 at 05:43

1 Answers1

1

You need to convert it to something that Linq understands:

public class Program
{
    [DllImport("kernel32.dll")]
    static extern uint GetCompressedFileSizeW(
        [In, MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
        [Out, MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);

    static void Main()
    {
        var x = GetFileSizeOnDisk("d:\\x.jpg");
        Console.WriteLine(x);
    }


    public static ulong GetFileSizeOnDisk(string file)
    {
        FileInfo info = new FileInfo(file);
        ulong clusterSize = 4096;

        var searcher = new ManagementObjectSearcher("select BlockSize,NumberOfBlocks from Win32_Volume WHERE DriveLetter = '" + info.Directory.Root.FullName.TrimEnd('\\') + "'");

        var mo = (searcher.Get() as ManagementObjectCollection).OfType<ManagementObject>().FirstOrDefault();

        if(mo != null)
            clusterSize = (ulong)mo["BlockSize"];

        uint hosize;
        uint losize = GetCompressedFileSizeW(file, out hosize);
        ulong size = hosize << 32 | losize;

        var res = ((size + clusterSize - 1) / clusterSize) * clusterSize;

        return res;
    }
}
Boppity Bop
  • 9,613
  • 13
  • 72
  • 151
  • For a second or two you really scared me :-) but, alas, nope, I have it (together with using System.Management plus reference in the project of it and System.Management.Instrumentation). Good point though.. – Nick Sep 07 '22 at 10:10
  • i think i had the same issue few years ago. let me see if i can repro. what is it? .net framework or .net core? which version? – Boppity Bop Sep 07 '22 at 10:31
  • .NET - Thank you very much ! I suspect something with the select.. I have replaced with code and seems I'm aaaaalmost there - I got an errorcode "InvalidQuery" (HResult -2146233087) and error message is same (invalid query). I don't know what to suppose.. – Nick Sep 07 '22 at 12:49
  • 1
    here is the full example. your longs/ints/ulongs were all messed up.. – Boppity Bop Sep 07 '22 at 12:52
  • Thank you very much ! you were right. I get the error code I think also for another reason, running the above I saw as unhandled exception that the info.Directory.Root.Fullname is "\\\\file2\desktops" - so I suppose that's why the select doesn't work and throws exception "Invalid query". Any ideas how to correct this? (so, my filename is \\file2\desktops\myuser\Desktop\TestDataFile.txt ) – Nick Sep 07 '22 at 13:15
  • thats a different question Nick – Boppity Bop Sep 07 '22 at 13:28