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.
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);
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)?
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.