在 C# 中为远程安装程序应用程序创建准确的进度条



这里对C#非常陌生,我学到了很多东西。

我正在创建一个远程安装补丁的 Winforms 应用程序。基本上,你给它一个文件(.msi或.exe)和一个计算机列表,然后它沿着列表向下,直到安装了所有补丁。它似乎正在为我想要它做的事情工作。只需单击一下即可进行漏洞/补丁管理。作为记录,我使用PSexec和powershell来做同样的任务,它们很棒。只是摆弄我自己的,希望在这个过程中做一些学习。

我想为应用程序创建一个准确的进度条,以便管理员可以大致了解当时正在处理哪些进程。可能需要修补的系统数量分布非常广泛,从 10 个系统到 1K+ 不等。

我看过进度条实现的教程,但大多数都是基于代码编写器估计特定作业的任务完成时间。由于每个补丁大小、安装时间和计算机数量都不同,这似乎对我没有多大帮助。

private void Start_Click(object sender, EventArgs e)
    {
        string complist = openFileDialog2.FileName;
        string patch = textBox2.Text;
        string fileName = patch;
        string patchfile = Path.GetFileName(fileName);
        foreach (string line in File.ReadLines(complist))
        {
            //Checks if C:PatchUpdates exists on remote machine. If not, a folder is created.
            if (!Directory.Exists(@"\" + line + @"C$PatchUpdates"))
            {
                Directory.CreateDirectory(@"\" + line + @"C$PatchUpdates");
            }
            //XCOPY patch to every computer
            System.Diagnostics.Process processCopy = new System.Diagnostics.Process();
            ProcessStartInfo StartInfo = new ProcessStartInfo();
            StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            StartInfo.FileName = "cmd";
            StartInfo.Arguments = string.Format("/c xcopy " + patch + " " + @"\{0}C$PatchUpdates /K /Y", line);
            processCopy.StartInfo = StartInfo;
            processCopy.Start();
            processCopy.WaitForExit();

            //Checks filename and installs according to file type.
            if (patch.Contains(".msi"))
            {
                //Uses WMI to execute a remote command to install using msiexec.
                ConnectionOptions options = new ConnectionOptions();
                options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
                ManagementScope WMIscope = new ManagementScope(
                string.Format("\\{0}\root\cimv2", line));
                WMIscope.Connect();
                ManagementClass WMIprocess = new ManagementClass(
                    WMIscope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
                object[] processmsi = { @"cmd.exe /c msiexec /qn /i " + @"C:PatchUpdates" + patchfile + @" /norestart" };
                object result = WMIprocess.InvokeMethod("Create", processmsi);
            }
            else if (patch.Contains(".exe"))
            {
                //Uses WMI to execute a remote command to install using commandline.
                ConnectionOptions options = new ConnectionOptions();
                options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
                ManagementScope WMIscope = new ManagementScope(
                    string.Format("\\{0}\root\cimv2", line));
                WMIscope.Connect();
                ManagementClass WMIprocess = new ManagementClass(
                    WMIscope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
                object[] processexe = { @"cmd.exe /c C:PatchUpdates" + patchfile + @" /silent /norestart" };
                object result = WMIprocess.InvokeMethod("Create", processexe);
            }
            else if (patch.Contains(".msu"))
            {
                //Uses WMI to execute a remote command to install using WUSA.
                ConnectionOptions options = new ConnectionOptions();
                options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
                ManagementScope WMIscope = new ManagementScope(
                    string.Format("\\{0}\root\cimv2", line));
                WMIscope.Connect();
                ManagementClass WMIprocess = new ManagementClass(
                    WMIscope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
                object[] processmsu = { @"wusa " + patchfile + " /quiet /norestart" };
                object result = WMIprocess.InvokeMethod("Create", processmsu);
            }
        }
    }

上面的代码是完成大部分工作的地方。当用户单击"开始"时,修补程序将复制到每台计算机上的 C:\PatchUpdates 中,并使用 WMI 进行安装。

我怎样才能制作一个进度条,该进度条基于完成每项任务所花费的时间计算,并在修补最后一台计算机时以 100% 完成?我假设需要做很多工作来做到这一点。

任何帮助,不胜感激。

您需要首先获取行数(您的系统计数)。

如果您使用的是 .Net 4.0 或更高版本,则可以使用

var lineCount = File.ReadLines(@"C:file.txt").Count();

否则你可以先做

var lineCount = 0;
using (var reader = File.OpenText(@"C:file.txt"))
{
    while (reader.ReadLine() != null)
    {
        lineCount++;
    }
}

之后,将进度条最小值设置为 0,将最大值设置为此系统计数。在foreach中,您只需要做一个进度条。执行步骤。

public void loadFiles()
{
   // Sets the progress bar's minimum value to a number representing
   // no operations complete -- in this case, no files read.
   progressBar1.Minimum = 0;
   // Sets the progress bar's maximum value to a number representing
   // all operations complete -- in this case, all five files read.
   progressBar1.Maximum = Convert.ToInt(lineCount); // in our case to the number of systems
   // Sets the Step property to amount to increase with each iteration.
   // In this case, it will increase by one with every file read.
   progressBar1.Step = 1;
   // Uses a for loop to iterate through the operations to be
   // completed. In this case, five files are to be copied into memory,
   // so the loop will execute 5 times.
   for (int i = 0; i <= 4; i++)
   {
      // Inserts code to copy a file
      progressBar1.PerformStep();
      // Updates the label to show that a file was read.
      label1.Text = "# of Files Read = " + progressBar1.Value.ToString();
   }
}

相关内容

最新更新