我正在尝试创建一个实用程序来整理我网络上的所有机器。我已经成功地使用了WMI的碎片整理和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
}发布于 2012-10-31 20:37:07
我可能只会使用PsExec远程运行该命令。这应该适用于几乎所有的Windows (NT)版本。
发布于 2012-11-01 20:16:11
为了完成这个帖子,我想我应该发布对我来说实际有效的代码,为了让这些代码正常工作,你必须下载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
}https://stackoverflow.com/questions/13157901
复制相似问题