【问题标题】:Dism unrecognized in CMD Shell in C#Dism 在 C# 的 CMD Shell 中无法识别
【发布时间】:2019-01-10 20:22:01
【问题描述】:

我正在尝试为我正在安装的另一个应用程序启用 dotnet 3.5 依赖项。问题是我正在使用的 cmd shell 抛出此错误“'dism' 未被识别为内部或外部命令, 可运行的程序或批处理文件。”但是如果我复制并粘贴我传递给 cmd shell 的字符串:

Dism /online /LogPath:C:\Users\HollyPlyler\source\repos\installerOptimized\installerOptimized\bin\Debug\\Logs\DSIMEnableDotNet.log /LogLevel:4 /Enable-Feature /FeatureName:NetFx3

效果很好。

这是我的 CMD 类:

 class CommandLineTool
    {
        public async Task Com(String command, string logName)
        {
            Console.WriteLine("received " + command);
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "CMD.exe";
            startInfo.Arguments = "/c " + command;
            process.StartInfo = startInfo;

            Task t = Task.Run(()=>process.Start());
            t.Wait();
            string output = process.StandardOutput.ReadToEnd();
            Console.BackgroundColor = ConsoleColor.Black;
            if (output.Contains("0/1"))
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                installerOptimized.install.success = false;
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Green;
                installerOptimized.install.success = true;
            }
            Console.WriteLine(output);
            string success = installerOptimized.install.success ? "successful" : "unsuccessful";
            System.IO.File.WriteAllLines("Logs/" + logName +"_" + success + ".txt", output.Split('\n'));
            process.WaitForExit();
    }
}

【问题讨论】:

  • 是否需要运行cmd.exe /c dism ... 而不仅仅是dism ...
  • 我可以试试。谢谢。
  • 似乎工作正常,但需要一段时间才能完成。一会儿我会告诉你的。
  • 不,只是永远挂起。也不适用于我需要的任何其他 cmd 命令。所以需要 /C。
  • 您是否查看了失败的 cmd shell 中的路径环境变量?

标签: c# cmd dism


【解决方案1】:

好的,我已经让它以这种方式工作了。通过为 DISM.exe 创建第二个类。我可能会合并它们并添加一个 varialbe 后者进行优化。

 class Dism
    {
        public async Task Com(String command, string logName)
        {
           Console.WriteLine("received " + command);
           System.Diagnostics.Process process = new System.Diagnostics.Process();
           System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
           startInfo.UseShellExecute = true;
           startInfo.CreateNoWindow = false;
           startInfo.RedirectStandardOutput = true;
           startInfo.UseShellExecute = false;
           startInfo.FileName = "Dism.exe";
           startInfo.Arguments = command;
        // startInfo.Arguments = command;
           process.StartInfo = startInfo;

           Task t = Task.Run(() => process.Start());
           t.Wait();
           string output = process.StandardOutput.ReadToEnd();
           Console.BackgroundColor = ConsoleColor.Black;
           if (output.Contains("0/1"))
           {
               Console.ForegroundColor = ConsoleColor.Yellow;
               installerOptimized.install.success = false;
           }
           else
           {
               Console.ForegroundColor = ConsoleColor.Green;
               installerOptimized.install.success = true;
           }
           Console.WriteLine(output);
           string success = installerOptimized.install.success ? "successful" : "unsuccessful";
           System.IO.File.WriteAllLines("Logs/" + logName + "_" + success + ".txt", output.Split('\n'));
           process.WaitForExit();
       }
   }

这样称呼它:

 myDism.Com("/Online /LogPath:log.log /LogLevel:4 /Enable-Feature /FeatureName:NetFx3 /All", "dism");

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多