0

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;
}
Rowandish
  • 2,655
  • 3
  • 31
  • 52
  • 1
    You can use WMI query to get that value as described [here](https://learn.microsoft.com/en-us/windows-hardware/customize/enterprise/uwfoverlay) and a side note you don't need administrative rights when you are just querying values. – Eldar May 15 '20 at 12:28
  • Are you trying to supply the argument `/user:Administrator` to `cmd.exe`? it doesn't support such an argument, so you should read the help text returned when typing `cmd /?` into a command prompt window... – aschipfl May 15 '20 at 13:00
  • @Eldar yes WMI should be the right thing but I could not find any proper documentation about it... I try with the code posted in the answer but with no success (method not implemented) – Rowandish May 15 '20 at 13:33
  • 1
    Try `var result = instance.Item["AvailableSpace"]` since `AvailableSpace` is not a method but property. – Eldar May 15 '20 at 14:13
  • @Eldar it works! Thank you. I added the full version of the code as a answer in order to help others. – Rowandish May 15 '20 at 15:14
  • Perhaps you will also find a suitable answer in my "experiment": https://github.com/cregx/uwf-dashboard – creg Sep 20 '21 at 14:49

1 Answers1

0

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());
}
Rowandish
  • 2,655
  • 3
  • 31
  • 52