用C#將WINDOWS8日志文件拷貝到別的盤,主要問題是不會調用帶有管理員權限的CMD命令,網上說的一些都不好使。附代碼,求大神指教!
private Process proc = null;
///
/// 構造方法
///
public Command()
{
proc = new Process();
}
///
/// 執行CMD語句
///
/// 要執行的CMD命令
public void RunCmd(string cmd)
{
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "C:\Windows\System32\cmd.exe";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.Verb = "RunAs";
proc.Start();
proc.StandardInput.WriteLine(cmd);
proc.Close();
}
public void ChangeFile(string path1,string path2)
{
Command cmd = new Command();
cmd.RunCmd("attrib -s" + " " + path1);
Directory.CreateDirectory(path2);//在path2路徑下創建文件夾
cmd.RunCmd("Xcopy" + " " + path1 + " " + path2);//把path1中的文件復制到path2中
}
如果你想在C#中以管理員新開一個進程,參考: Run process as administrator from a non-admin application
ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\cmd.exe");
info.UseShellExecute = true;
info.Verb = "runas";
Process.Start(info);
如果你想在命令行加參數,可以參考: Running CMD as administrator with an argument from C#
Arguments = "/user:Administrator \"cmd /K " + command + "\""