检查 .NET 3.5 并在禁用时启用

Check for .NET 3.5 and enable if disabled

我有一个运行以下行的批处理文件:

dism /online /get-features /format:table | find "NetFx3"

如果NetFx3的输出returns是'disabled'我想用下面的命令启用它:

dism.exe /online /enable-feature /featurename:NetFX3

我不确定如何实际查询输出并检查 enabled/disabled。

使用 dot-net 代码可以轻松完成。作为,

  1. 使用适当的参数启动 Dism 进程。
  2. 不要使用 ShellExecute。
  3. 重定向标准输出。
  4. 读取标准输出。

Vb.net代码:

Public Function GetDismOutput() As String
        Dim Output As String = Nothing
        Dim SxsPath As String = "D:\Sources\sxs"
        Dim CmdParm As String = "/online /enable-feature /featurename:NetFX3 /All /Source:"""
        Dim Pinf As New ProcessStartInfo With {.FileName = SxsPath, .Arguments = CmdParm + SxsPath + """ /LimitAccess"}
        Dim Prc As Process
        Try
            Pinf.UseShellExecute = False
            Pinf.RedirectStandardOutput = True
            Prc = Process.Start(Pinf)
            Prc.WaitForExit(20 * 60 * 1000) '' Wait for 20 Minutes
            CmdParm = Prc.StandardOutput.ReadToEnd()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Output
    End Function

C#代码:

  public string GetDismOutput()
  {
        string Output = null;
        string SxsPath = "D:\Sources\sxs";
        string CmdParm = "/online /enable-feature /featurename:NetFX3 /All /Source:\"";
        ProcessStartInfo Pinf = new ProcessStartInfo {FileName = SxsPath, Arguments = CmdParm + SxsPath + "\" /LimitAccess"};
        Process Prc = null;
        try
        {
            Pinf.UseShellExecute = false;
            Pinf.RedirectStandardOutput = true;
            Prc = Process.Start(Pinf);
            Prc.WaitForExit(20 * 60 * 1000); //' Wait for 20 Minutes
            CmdParm = Prc.StandardOutput.ReadToEnd();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return Output;
    }

其中D:\Sources\sxs是sxs文件夹的路径。 输出字符串将类似于:

Deployment Image Servicing and Management tool
Version: 6.3.9600.17031
Image Version: 6.3.9600.17031
Enabling feature(s)
The operation completed successfully.