none
Drive Info - Code improvements RRS feed

  • Question

  • I'm trying to see if there is a better way of making this better.

            public int listDrives( long size,out DriveInfo[] Drives)
            { // if size is not same will return the driveinfo
                Drives = null;
                try
                {
                    DriveInfo[] temp = DriveInfo.GetDrives();
                    if (size != Marshal.SizeOf(temp))
                    {
                        Drives = temp;
                        return 1;
                    }
                    return 0;
                }
                catch (Exception ex)
                {
                    return ex.HResult;
                }             
            }
            DriveInfo[] Drives;
            public void RefreshDirectories()
            {
                if (Fm.listDrives(Marshal.SizeOf(Drives), out Drives) == 1)
                {
                    //process new drive info
                }
            }
    What it is to do is to be able to call the function listDrives and if the current DriveInfo is the not the same as the on in memory than it will update the DriveInfo.


    Dim Blondedude092 as SkypeUser

    Saturday, May 7, 2016 12:43 AM

Answers

  • You'll have to compare the relevant DriveInfo properties if you want to detect drive info changes. Marshal.SizeOf doesn't belong in this code and probably doesn't even work at runtime, you'll likely get an exception if you such code.
    Saturday, May 7, 2016 7:31 AM
    Moderator
  • Hi Dmitry Post,

    >> I'm trying to see if there is a better way of making this better.

    If you want to compare the DriveInfo Size, you should accept Mike Danes’s suggestion to compare the DriveInfo properties. Based on Mike Danes’s suggestion, I modify your code as below and you could have a look. In the code, I use DriveInfo.TotalSize Property as the comparing object. If it’s not what you want, please let me know and provide more things to clarify your intentions.

        public int listDrives(DriveInfo di, out DriveInfo[] Drives)
            { // if size is not same will return the driveinfo
                Drives = null;
                try
                {
                    DriveInfo[] temp = DriveInfo.GetDrives();
                    foreach (DriveInfo item in temp)
                    {
                        //The compare logic should be changed according to your need
                        //Note, you should first make the "current item" is available
                        if (item.IsReady && item.TotalSize == di.TotalSize)
                        {
                            Drives = temp;
                            return 1;
                        }
                    }
                    return 0;
                }
                catch (Exception ex)
                {
                    return ex.HResult;
                }
            }
    
    
            DriveInfo[] Drives;
            public void RefreshDirectories()
            {
                //Here, the "myDI" is a test value, you could change it according to your need.
                DriveInfo myDI = new DriveInfo("C");
    
                if (Fm.listDrives(myDI, out Drives) == 1)
                {
                    //process new drive info
                }
            }

    >>What it is to do is to be able to call the function listDrives and if the current DriveInfo is the not the same as the on in memory than it will update the DriveInfo.

    The DriveInfo.GetDrives Method () retrieves the drive names of all logical drives on a computer and the result is an array of type DriveInfo. So, you should check whether current DriveInfo is “in” the all logical drives or not, not is equaling to the result of DriveInfo.GetDrives Method ().

    Best Regards,

    Albert Zhang



    Monday, May 9, 2016 8:52 AM

All replies

  • You'll have to compare the relevant DriveInfo properties if you want to detect drive info changes. Marshal.SizeOf doesn't belong in this code and probably doesn't even work at runtime, you'll likely get an exception if you such code.
    Saturday, May 7, 2016 7:31 AM
    Moderator
  • Hi Dmitry Post,

    >> I'm trying to see if there is a better way of making this better.

    If you want to compare the DriveInfo Size, you should accept Mike Danes’s suggestion to compare the DriveInfo properties. Based on Mike Danes’s suggestion, I modify your code as below and you could have a look. In the code, I use DriveInfo.TotalSize Property as the comparing object. If it’s not what you want, please let me know and provide more things to clarify your intentions.

        public int listDrives(DriveInfo di, out DriveInfo[] Drives)
            { // if size is not same will return the driveinfo
                Drives = null;
                try
                {
                    DriveInfo[] temp = DriveInfo.GetDrives();
                    foreach (DriveInfo item in temp)
                    {
                        //The compare logic should be changed according to your need
                        //Note, you should first make the "current item" is available
                        if (item.IsReady && item.TotalSize == di.TotalSize)
                        {
                            Drives = temp;
                            return 1;
                        }
                    }
                    return 0;
                }
                catch (Exception ex)
                {
                    return ex.HResult;
                }
            }
    
    
            DriveInfo[] Drives;
            public void RefreshDirectories()
            {
                //Here, the "myDI" is a test value, you could change it according to your need.
                DriveInfo myDI = new DriveInfo("C");
    
                if (Fm.listDrives(myDI, out Drives) == 1)
                {
                    //process new drive info
                }
            }

    >>What it is to do is to be able to call the function listDrives and if the current DriveInfo is the not the same as the on in memory than it will update the DriveInfo.

    The DriveInfo.GetDrives Method () retrieves the drive names of all logical drives on a computer and the result is an array of type DriveInfo. So, you should check whether current DriveInfo is “in” the all logical drives or not, not is equaling to the result of DriveInfo.GetDrives Method ().

    Best Regards,

    Albert Zhang



    Monday, May 9, 2016 8:52 AM