使用C#跟踪DISM进度-在Windows 10中工作但在WinPE中工作

时间:2019-05-05 14:52:06

标签: c# winpe dism

我正在使用C#启动DISM过程以捕获/应用图像并跟踪进度。

当我在Windows 10上运行代码时,进度会正常更新,但是当我在WinPE环境中运行代码时,进度不会更新,但进程仍会正常运行。

这是触发方法启动DISM的代码。 只是要注意它正在.NET Framework 4.0上运行

Task T = Task.Factory.StartNew(() =>
                            {
                                ApplyImage(machineName, imageIndex, imageDescription);
                                //Utilities.BailOut();
                            });

这是我用来启动DISM的代码,参数根据需要而改变。

     public void ApplyImage(string imageName, int imageIndex)
            {
                string _imageName = imageName;
                int _index = imageIndex;
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
                {
                    UseShellExecute = true,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    FileName = "Dism.exe",
                    Arguments = $@"/apply-image /imagefile:{OS_Deploy}\{_imageName} /index:{_index} /applydir:{TargetDisk}"
                };
                startInfo.UseShellExecute = false;
                process.StartInfo = startInfo;
                process.OutputDataReceived += (sender, e) => setLabelText(lblProgress, e.Data);
                process.Start();
                process.BeginOutputReadLine();

                process.WaitForExit();
                process.CancelOutputRead();
                process.Close();
            }

这是设置标签文本的代码

private void setLabelText(Label label, string text)
        {
            if (label.InvokeRequired)
            {
                label.Invoke((System.Action)(() => setLabelText(label, text)));
            }
            else
            {
                try
                {
                    if (text.Contains("%"))
                    {
                        label.Text = "Status : " + text.Split('%')[0].Substring(text.Split('%')[0].Length - 4, 4) + "%";
                    }
                    else if (text == "The operation completed successfully.")
                    {
                        label.Text = "Completed!";
                    }
                    else
                    {
                        label.Text = text;
                    }
                }
                catch (Exception) { }
            }
        }

什么可能导致此问题,我该如何解决或创建解决办法?

0 个答案:

没有答案