我正在从我的c#应用程序调用Defrag.exe。我希望在更新后获得碎片整理过程的输出。
我希望在我的应用程序中打印此输出(红色矩形)。(当更新11%的进度百分比时,也应打印它。):
我正在使用此代码:
Process selectedProc = new Process();
selectedProc.StartInfo.UseShellExecute = false;
selectedProc.StartInfo.RedirectStandardOutput = true;
selectedProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
selectedProc.StartInfo.CreateNoWindow = true;
selectedProc.OutputDataReceived += (sender, e) =>
{
Debug.WriteLine(e.Data);
};
selectedProc.StartInfo.WorkingDirectory = System.IO.Path.GetPathRoot(Environment.SystemDirectory);
selectedProc.StartInfo.FileName = System.IO.Path.Combine(Environment.SystemDirectory, "defrag.exe");
selectedProc.StartInfo.Arguments = "C: /U";
selectedProc.Start();
selectedProc.BeginOutputReadLine();
过了一段时间我用这段代码关闭了流程:
selectedProc.Kill();
在process.Start()和process.Kill()之间,我没有在Visual Studio的调试部分看到任何输出。碎片整理过程结束后甚至都没有。问题在哪里?
答案 0 :(得分:0)
你可能需要某种api来查看那个过程 http://blogs.msdn.com/b/jeffrey_wall/archive/2004/09/13/229137.aspx
然后 http://msdn.microsoft.com/en-us/library/windows/desktop/aa363911%28v=vs.85%29.aspx
答案 1 :(得分:0)
您需要以管理员身份运行cmd并传递参数以运行碎片整理
namespace System.IO.Defrag
{
using System.Diagnostics;
using System.Text.RegularExpressions;
public class Defrag
{
private Process ownprocess;
public void Stop()
{
ownprocess?.Kill();
}
private IDictionary<Operations, string> vOperations = new Dictionary<Operations, string>()
{
{Operations.Analyze, "/A "},{ Operations.BootOptimize,"/B "},{ Operations.Defrag ,"/D "},
{Operations.Optimize ,"/O "},{ Operations.FreespaceConsolidate,"/X "},{ Operations.PrintProgress,"/U "},
{Operations.Retrim,"/L " },{ Operations.SlabConsolidate ,"/K "},{ Operations.TierOptimize,"/G "},
{Operations.TrackProgress ,"/T "},{ Operations.Verbose,"/V"}
};
private IDictionary<Options, string> vOptions = new Dictionary<Options, string>()
{
{Options.MultiThread , "/M" },{ Options.MaxRuntime, "I"},{ Options.NormalPriority , "/H"},
{ Options.None,""}
};
public enum Options
{
NormalPriority,
MaxRuntime,
MultiThread,
None
}
public enum Operations
{
Analyze,
BootOptimize,
Defrag,
TierOptimize,
SlabConsolidate,
Retrim,
Optimize,
TrackProgress,
PrintProgress,
Verbose,
FreespaceConsolidate
}
private string English = "437";
public event DataReceivedEventHandler OutputData;
public T GetValue<T>(string data)
{
object o = new object();
T a = (T)o;
if (a is string)
{
Regex he = new Regex(": (.*?)% ");
var j = he.Match(data);
o = j.Groups[1].Value;
a = (T)o;
}
else if (a is int)
{
Regex he = new Regex(": (.*?)% ");
var j = he.Match(data);
o = Convert.ToInt32(j.Groups[1].Value);
a = (T)o;
}
else if (a is double)
{
Regex he = new Regex(": (.*?)% ");
var j = he.Match(data);
o = Convert.ToDouble(j.Groups[1].Value);
a = (T)o;
}
else
{
MessageBox.Show("This format is not supported.",$"Error 0x{Encoding.UTF8.GetBytes(a.GetType().Name).Length}",MessageBoxButton.OK,MessageBoxImage.Error);
}
return a;
}
private void cmd(string arg)
{
Process process = new Process();
process.StartInfo = new ProcessStartInfo("cmd",$"/c start \"\" chcp {English} && start \"\" {arg}");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (OutputData != null)
{
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += OutputData;
ownprocess = process;
process.Start();
process.BeginOutputReadLine();
}else
{
ownprocess = process;
process.Start();
}
}
public async void Start(string Volumes = "C:", Operations dOparations = Operations.Defrag, Options options = Options.None)
{
await Task.Factory.StartNew(() =>
{
cmd($"defrag {Volumes} {vOperations[dOparations]}{vOptions[options]}");
});
}
}
}