C#中CMD Shell中无法识别Dism

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中CMD Shell中无法识别Dism相关的知识,希望对你有一定的参考价值。

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

Dism /online /LogPath:C:UsersHollyPlylersource
eposinstallerOptimizedinstallerOptimizedinDebug\LogsDSIMEnableDotNet.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('
'));
            process.WaitForExit();
    }
}
答案

好的,我已经以这种方式工作了。通过为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('
'));
           process.WaitForExit();
       }
   }

像这样称呼它:

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

谢谢。

以上是关于C#中CMD Shell中无法识别Dism的主要内容,如果未能解决你的问题,请参考以下文章

我的 DISM 命令似乎运行成功,但没有安装

我的片段中的按钮无法识别

linux下如何用C代码执行一个cmd命令,然后可以控制该命令的结束

求问 BAT脚本如何自动执行 adb shell 以后的命令

无法识别 C++ CreateProcess 'telnet'

Emacs shell 无法识别 python 命令