简体   繁体   中英

How to get Windows UWF available space in C#

I'd like to run the following shell command in a C# program:

uwfmgr overlay get-availablespace

One simple solution would be call a cmd process like this:

var ps = new ProcessStartInfo("cmd")
{
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    FileName = "cmd.exe",
    Arguments = "/user:Administrator /c uwfmgr overlay get-availablespace"
};

using (Process p = Process.Start(ps))
{
    output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
}

But is there a simple API to call in order to avoid to parse the output string of the process? Another problem is that I have to call cmd with administrative rights.

UPDATE: I try with WMI Api but with no success, here is my code:

var scope = new ManagementScope(@"root\standardcimv2\embedded");
var uwfClass = new ManagementClass(scope.Path.Path, "UWF_Overlay", null);
foreach (ManagementObject instance in uwfClass.GetInstances())
{
    var result = instance.InvokeMethod("AvailableSpace", null);
    break;
}

Here is the working code to obtain UWF AvailableSpace and OverlayConsumption .

var scope = new ManagementScope(@"root\standardcimv2\embedded");
var uwfClass = new ManagementClass(scope.Path.Path, "UWF_Overlay", null);
foreach (ManagementObject instance in uwfClass.GetInstances())
{
    var availableSpace = Convert.ToInt32(instance.GetPropertyValue("AvailableSpace").ToString());
    var overlayConsumption = Convert.ToInt32(instance.GetPropertyValue("OverlayConsumption").ToString());
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM