Reputation: 1325
How to add progressbar when excuting the bat file?
I have c# code below:
private void button2_Click(object sender, EventArgs e)
{
string path = "C:\\Users\\Public\\Downloads\\install_ubuntu2";
string batFileName = path + ".bat";
using (StreamWriter batFile = new StreamWriter(batFileName))
{
batFile.WriteLine("wsl.exe -u root apt update");
batFile.WriteLine("wsl.exe -u root apt --assume-yes install xwayland");
batFile.WriteLine("wsl.exe -u root apt --assume-yes install libegl-dev");
batFile.WriteLine("wsl.exe -u root DEBIAN_FRONTEND=noninteractive apt --assume-yes install lightdm");
batFile.WriteLine("wsl.exe -u root apt --assume-yes install ubuntu-desktop");
batFile.WriteLine("wsl.exe -u root wget -O /tmp/install-sg.sh --content-disposition \"https://gist.githubusercontent.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950/raw/952347f805045ba0e6ef7868b18f4a9a8dd2e47a/install-sg.sh\"");
batFile.WriteLine("wsl.exe -u root chmod +x /tmp/install-sg.sh");
batFile.WriteLine("wsl.exe -u root /tmp/install-sg.sh && rm /tmp/install-sg.sh");
batFile.WriteLine("wsl.exe -u root sed -i 's/240/3/g' /etc/genie.ini");
batFile.WriteLine("wsl.exe --shutdown");
batFile.WriteLine("wsl whoami > test1.txt");
batFile.WriteLine("set /P Var=<test1.txt ");
batFile.WriteLine("Set Var1=/home/%Var%/.bashrc");
batFile.WriteLine("wsl.exe -u root echo \"genie -i\" ^>^> \"%Var1%\"");
batFile.WriteLine("wsl.exe -u root echo \"genie -c Xwayland & \" ^>^> \"%Var1%\"");
batFile.WriteLine("wsl.exe -u root echo \"timeout 20\" ^>^> \"%Var1%\"");
batFile.WriteLine("wsl.exe -u root echo \"genie -c gnome-session & \" ^>^> \"%Var1%\"");
batFile.WriteLine("powershell -Command \"Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser\"");
batFile.WriteLine("powershell -Command \"Set-ExecutionPolicy RemoteSigned -Scope CurrentUser\"");
batFile.WriteLine("powershell -Command \"Install-Module VirtualDesktop -Force -Scope CurrentUser\"");
batFile.WriteLine("powershell -Command \"Import-Module VirtualDesktop\"");
batFile.WriteLine("powershell -Command \"New-Desktop\"");
//batFile.WriteLine("PAUSE");
}
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/c " + batFileName);
processStartInfo.UseShellExecute = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
Process p = new Process();
p.StartInfo = processStartInfo;
p.Start();
p.WaitForExit();
File.Delete(batFileName);
}
I have to know to cmd execution progress and add a progressbar.
The progressbar's code is enter link description here.
How to achieve that?
Thank you very much.
Upvotes: 0
Views: 355
Reputation: 142
In Windows Forms you could do something like this (In this version without the use of IProgress
):
void StartButton_Click(object sender, EventArgs e)
{
var listOfCommands = InitializeListOfCommands();
var numberOfItems = listOfCommands.Count();
for (int i = 0; i < numberOfItems; i++)
{
RunCommand(listOfCommands[i].Cmd, listOfCommands[i].Parameter);
ProgressProgressBar.Value = (i + 1) * 100 / numberOfItems;
}
}
List<(string Cmd, string Parameter)> InitializeListOfCommands()
{
return new List<(string Cmd, string Parameter)>()
{
("wsl.exe", "-u root apt update"),
("wsl.exe", "-u root apt --assume-yes install xwayland"),
// next commands
};
}
void RunCommand(string cmd, string arguments)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(cmd, arguments);
processStartInfo.UseShellExecute = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
Process p = new Process();
p.StartInfo = processStartInfo;
p.Start();
p.WaitForExit();
}
Keep in mind:
Upvotes: 1