繁体   English   中英

Windows碎片整理无法识别为内部或外部命令,可操作程序或批处理文件

[英]Windows defrag is not recognized as an internal or external command, operable program or batch file

我正在开发一个技术工具包程序,此“工具包”中将包含一个在本地磁盘上运行碎片整理的按钮。 目前,我为此创建的批处理文件很简单,它仅运行基本的碎片分析:

defrag C: /A

触发此操作的按钮后面的C#代码为:

System.Diagnostics.ProcessStartInfo procInfo = 
    new System.Diagnostics.ProcessStartInfo();
procInfo.Verb = "runas";
procInfo.FileName = "(My Disk):\\PreDefrag.bat";
System.Diagnostics.Process.Start(procInfo);

该代码完全符合我的要求,它调用UAC,然后使用管理特权启动我的批处理文件。 尽管运行了批处理文件,但是我收到的输出到命令控制台是:

C:\Windows\system32>defrag C: /A
'defrag' is not recognized as an internal or external command, 
    operable program or batch file.

是什么导致此错误,我该如何解决?

检查以确保defrag.exe文件确实存在于C:\\ Windows \\ System32中。

尝试将'defrag'命令完全限定为:

C:\WINDOWS\system32\defrag.exe C: /A

打开一个cmd提示符并运行以下命令: defrag.exe /? 并在问题中张贴您得到什么。

首先:将您的项目Platform Target属性设置为Any CPU并取消选中Prefer 32-bit选项(这是99.9%的问题)。 然后...为什么在可以执行批处理时启动一个调用该命令的批处理?

ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = "/C defrag C: /A";
info.FileName = "cmd.exe";
info.UseShellExecute = false;
info.Verb = "runas";
info.WindowStyle = ProcessWindowStyle.Hidden;

Process.Start(info);

在我的机器上像魅力一样工作。 对于多个命令:

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;

Process cmd = Process.Start(info);

using (StreamWriter sw = p.StandardInput)
{
    if (sw.BaseStream.CanWrite)
    {
        sw.WriteLine(command1);
        sw.WriteLine(command2);
        // ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM