C#远程运行defrag.exe

标签 c# wmi

我正在尝试创建一个实用程序来对我网络上的所有机器进行碎片整理。我已成功使用 WMI 的 Defrag 和 DefragAnalysis 方法,但它们与 Windows XP 不兼容。这是一个问题,因为我们在网络上有一些 XP 机器。 我已经能够在 XP 机器上本地调用 defrag.exe 进程来执行碎片整理,但是我在远程机器上调用它时遇到问题。下面是我在本地工作的代码,有人可以帮我让我的网络上的远程机器工作吗?我曾尝试使用一些 WMI 来提供帮助,但由于我是 C# 和 WMI 的新手,所以我没有成功,谢谢!

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "defrag";
info.Arguments = volume + " -f";
info.UseShellExecute = false;
info.CreateNoWindow = true;
info.RedirectStandardOutput = true;

Process defrag = Process.Start(info);
defrag.PriorityClass = ProcessPriorityClass.BelowNormal;

while (!defrag.HasExited)
{
    System.Threading.Thread.Sleep(1000);
    Process[] procs = Process.GetProcessesByName("dfrgntfs");
    if (procs != null && procs.Length > 0)
    {
        procs[0].PriorityClass = ProcessPriorityClass.Idle;
        defrag.WaitForExit();
    }

    result = null;
    while(!defrag.StandardOutput.EndOfStream)
    {
        //get output and store results
    }

最佳答案

为了完成这个线程,我想我会发布对我实际有效的代码,要使此代码有效,您必须下载 PsTools 并将其放在根目录中...

            Process psexec = new Process();

            psexec.StartInfo.FileName = @"C:\PsExec.exe";
            psexec.StartInfo.Arguments = @"-s \\" + machine + " defrag.exe " + volume + " -f";
            psexec.StartInfo.UseShellExecute = false;
            psexec.StartInfo.CreateNoWindow = true;
            psexec.StartInfo.RedirectStandardOutput = true;

            psexec.Start();

            while (!psexec.HasExited)
            {
                System.Threading.Thread.Sleep(1000);

                Process[] procs = Process.GetProcessesByName("dfrgntfs", @"\\" + machine);
                if (procs != null && procs.Length > 0)
                {
                    psexec.WaitForExit();
                }

                while (!psexec.StandardOutput.EndOfStream)
                {
                    //get output and store results
                }

关于C#远程运行defrag.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13157901/

相关文章:

c# - .NET 3.5 中 Enum.TryParse 的实现

C#如何在文本框为空时防止按下Tab键

c# - 在输入文件中找不到字符串

c# - 无法使用 WMI 终止进程,但 taskkill 可以工作

c# - 如何在C#中查询NTFS磁盘配额?

c# - 使用 Streamreader.Read block /RedirectStandardOutput

C#,在List<>.ConvertAll方法中使用lambda表达式,当对象为null时如何进行过滤?

c# - 使用 C# 以编程方式更改 ipsec 规则的方法?

c# - RPC 服务器不可用。 (HRESULT : 0x800706BA) when connecting to remote computer 的异常

windows - 用于检查系统中已安装程序及其版本的脚本