Dough Dough - 27 days ago 11
C# Question

GUI for powercfg battery report

I'm trying to create a GUI for the powercfg battery report. I've written the below, but it doesn't seem to work. Can anyone help please?

Thanks

private void button1_Click(object sender, EventArgs e)
{
var saveDialog = new SaveFileDialog();
saveDialog.OverwritePrompt = true;
saveDialog.Title = "Save your battery report";
saveDialog.Filter = "HTML | *.html";
var defaultSavePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
saveDialog.InitialDirectory = defaultSavePath;
saveDialog.ShowDialog();
var saveLocation = saveDialog.FileName;

if (saveLocation != null)
{
saveLocation = "\"" + saveLocation + "\"";
var startInfo = new ProcessStartInfo();
startInfo.FileName = "powercfg.exe";
startInfo.Arguments = " /batteryreport /output " + saveLocation;
startInfo.CreateNoWindow = false;
startInfo.ErrorDialog = true;
startInfo.UseShellExecute = true;
Process.Start(startInfo);
}
}

Answer

Since my OS is 64 bit, in project properties I un-checked Prefer 32-bit checkbox in Build tab and it worked properly.

  1. Right click on Project → Properties
  2. Build Tab → General
  3. Set Platform Target combo box : Any CPU, Prefer 32-bit check box : unchecked

It also works when you choose the same bitness as your OS for Platform Target. For example X4.