【问题标题】:Problem with running batch files from within my application从我的应用程序中运行批处理文件的问题
【发布时间】: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

标签: c# windows shell command


【解决方案1】:

这就是我解决问题的方法:

  1. 如果您打算在 64 位系统上使用您的应用程序,请确保编译为 64 位,否则它将重定向到不同的子文件夹并且找不到“wbadmin.exe”。

  2. 使用 ProcessStart 运行 wbadmin 或运行批处理但无需直接输入 cmd,因此将其与 filename = batch file 或 wbadmin 与 startInfo.Arguments 一起使用:

        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();
    
  3. 确保您请求管理员权限

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多