【发布时间】:2019-04-18 17:26:45
【问题描述】:
我一直在尝试创建一个简单的应用程序来备份我的 Windows Server 数据库以及整个服务器的备份。 为此,我想使用我的应用程序正在执行的批处理文件。 我尝试了几种方法,但由于某种原因它总是失败,所以如果你能帮助我,我会很高兴。
批处理文件备份:
wbadmin start backup -backupTarget:D: -include:C: -allCritical -quiet
我必须以管理员身份运行 bat,否则由于缺少权限而失败。
C#代码:
static Task<int> RunProcessAsync(string fileName)
{
............
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Verb = "runas";
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C \"D:\\SQLBACKUP\\BACKUPSERVER.bat\"";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
调试显示“未找到 wbadmin”。 'runas' 激活与否没有任何区别。
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fileName;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = false;
// startInfo.Verb = "runas";
var process = new Process
{
StartInfo = { FileName = fileName },
EnableRaisingEvents = true
};
process.StartInfo = startInfo;
process.Exited += (sender, args) =>
{
tcs.SetResult(process.ExitCode);
process.Dispose();
};
process.Start();
也不行。
有什么想法吗?
编辑: 我可以运行诸如关机之类的命令,但 wbadmin 无论如何都不起作用...
【问题讨论】:
-
乍一看,这似乎是一个工作目录问题。您的 .NET exe 将尝试从其当前工作目录中执行批处理文件。因此,除非 wbadmin 在该工作目录中,或者在全局 PATH 中,否则不会找到它。
-
我尝试将 .exe 放入 System32 文件夹,但也没有成功
-
默认工作目录很少是 System32 文件夹。 ProcessStartInfo 上有一个名为 WorkingDirectory 的属性。将其设置为 wbadmin 工具的绝对路径。
-
找不到命令“C:\Windows\System32\wbadmin.exe” C:\Windows\System32>C:\Windows\System32\wbadmin.exe 开始备份 -backupTarget:D: -include :C: -allCritical -quiet