none
Issue with Size property of FolderItem in Shell32 RRS feed

  • Question

  • Hi,

    I'm using "Interop.Shell32" for InteropServices and “FolderItem” to iterate through all items in a particular “Folder”. "Size" property in “FolderItem” is type of “int” and gives back wrong(-ve) value in case of large files.

    How can I handle this? Please suggest.<o:p></o:p>

    Below is the code -

     var shellApplication = "Shell.Application";
                var nameSpace = "NameSpace";
    
                var shellApplicationType = Type.GetTypeFromProgID(shellApplication);
                var shell = Activator.CreateInstance(shellApplicationType);
                Folder testFolder = (Folder)shell.GetType().InvokeMember(nameSpace, System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { 10 });
    
                foreach (FolderItem item in testFolder.Items())
                {
    
                    var Path = item.Name;
    
                    //Size propety is type of int and gives -ve value in case of large files
                    var intSize = item.Size;
                }

    Thanks




    Monday, July 23, 2018 1:45 PM

Answers

  • FolderItem is limited to 2GB files unless you treat it as unsigned in which it is limited to 4GB (the Size property is defined as int). Is there any reason you are using the shell rather than using the standard file system API?

    To work around this limitation use the ExtendedProperty instead.

    //Add reference to Shell32 COM object
    
    [STAThread]
    static void Main ( string[] args )
    {
        //var app = Type.GetTypeFromProgID("Shell.Application");
        //var shell = Activator.CreateInstance(app);
        var app = new Shell32.Shell();
    
        //var testFolder = (Folder)shell.GetType().InvokeMember("Namespace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { 10 });
        var testFolder = app.NameSpace(@"MyPath");
    
        foreach (FolderItem2 item in testFolder.Items())
        {
            var longSize = (ulong)item.ExtendedProperty("Size");
    
            Console.WriteLine($"{item.Name} = {item.Size}, {longSize}");                
        };
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 25, 2018 6:14 PM
    Moderator

All replies

  • >                //Size propety is type of int and gives -ve value in case of large files

    Give us an example - what is the actual size of a file that results in a -ve value?

    Is the value perhaps correct if you treat it as an unsigned value?

    Dave

    Monday, July 23, 2018 2:47 PM
  • Hi,

    Actual file size is 2564476928 in bytes and Size property gives back -1730490368 bytes.

    Thanks
    Sham
    Wednesday, July 25, 2018 2:21 PM
  • That's 0x‭98DAD000‬ which is ‭-1730490368‬ if represented as a signed 32-bit value.

    Treat is as an unsigned value.

    You'll presumably still have issues with files that are > 4GB though. It might be best to look at using some different interface which will handle large file sizes.

    Wednesday, July 25, 2018 2:48 PM
  • FolderItem is limited to 2GB files unless you treat it as unsigned in which it is limited to 4GB (the Size property is defined as int). Is there any reason you are using the shell rather than using the standard file system API?

    To work around this limitation use the ExtendedProperty instead.

    //Add reference to Shell32 COM object
    
    [STAThread]
    static void Main ( string[] args )
    {
        //var app = Type.GetTypeFromProgID("Shell.Application");
        //var shell = Activator.CreateInstance(app);
        var app = new Shell32.Shell();
    
        //var testFolder = (Folder)shell.GetType().InvokeMember("Namespace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { 10 });
        var testFolder = app.NameSpace(@"MyPath");
    
        foreach (FolderItem2 item in testFolder.Items())
        {
            var longSize = (ulong)item.ExtendedProperty("Size");
    
            Console.WriteLine($"{item.Name} = {item.Size}, {longSize}");                
        };
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 25, 2018 6:14 PM
    Moderator