2

I am creating a UWP app (C#, Windows, Visual Studio 2019). I want to list the available space, total space and total free space of the logical drives (for example, the drives are C, D, E). In order to do this, I have used DLLs that contain GetDiskFreeSpaceExA. I tried 2 things: DllImport with kernel32.dll and DllImport with a DLL file that I have created.

  1. Firstly, I have called this function using kernel32.dll

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    
    [return: MarshalAs(UnmanagedType.Bool)]
    
    static extern bool GetDiskFreeSpaceEx(String drivePath,
                                          out ULARGE_INTEGER availableBytes,
                                          out ULARGE_INTEGER totalBytes,
                                          out ULARGE_INTEGER totalFreeBytes);
    
  2. Secondly, I have created a C DLL using Windows Desktop Wizard and added it to the UWP app. The name of the DLL file is GetMemory.dll. The function GetSpaceData calls GetDiskFreeSpaceExA

    [DllImport("GetMemory.dll", EntryPoint = "GetSpaceData", CallingConvention = CallingConvention.Cdecl)]

    internal static extern int GetSpaceData(string drivePath, out ULARGE_INTEGER availableBytes, out ULARGE_INTEGER totalBytes, out ULARGE_INTEGER totalFreeBytes);

This is the C file used to create the DLL file:

__declspec(dllexport) int GetSpaceData(char* path, 
                                       ULARGE_INTEGER *ulFreeForUser, 
                                       ULARGE_INTEGER *ulTotal,
                                       ULARGE_INTEGER *ulFree)
{
        if (GetDiskFreeSpaceExA(path, ulFreeForUser, ulTotal, ulFree))
        {
            return 1;
        }
        return 0;
}

In both cases, I can only access the data about C drive (I have to access space data about E and D drives, too): the result from the functions is true and != 0 for "C:" and false and == 0 for "D:" and "E:".

I have also tried to access them using the C# code in UWP, but failed. Getting the storage data about the logical drives, using dlls and GetDiskFreeSpaceExA, in a C# Console App works.

Why can I only access the data about C drive (UWP)? How can I access the data for all of the logical drives (UWP)?

tp123
  • 47
  • 6
  • 1
    Please take the [tour], read [ask], post an [mcve] and [format your code](https://stackoverflow.com/editing-help#syntax-highlighting). – jwdonahue Jan 16 '21 at 22:44
  • 1
    How did you define `ULARGE_INTEGER`? That shouldn’t be valid in a p/Invoke definition. – Mitch Jan 17 '21 at 04:09
  • I used `Microsoft.VisualStudio.OLE.Interop` [link](https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.ole.interop?view=visualstudiosdk-2019) – tp123 Jan 17 '21 at 12:57

1 Answers1

1

You could add the broadFileSystemAccess capability in Package.appxmanifest.
In addition, broadFileSystemAccess is a restricted capability. You need to configure in Settings > Privacy > File system.

Then please refer to the answer of this question, the description of this solution as follows:

  1. Call DriveInfo.GetDrives() to retrieve the drive names of all logical drives on a computer.
  2. Traverse all Drives and use StorageFolder.GetFolderFromPathAsync() to get the root folder of each disk.
  3. Get the specified properties through
    StorageItemContentProperties.RetrievePropertiesAsync().
dear_vv
  • 2,350
  • 1
  • 4
  • 13