简体   繁体   English

C#控制台应用程序无法运行批处理文件,但批处理文件在命令提示符下工作正常

[英]C# console app can't run a batch file but batch file works fine in command prompt

I have a batch file which runs perfectly fine if I give this command in the command prompt. 我有一个批处理文件,如果我在命令提示符下给出此命令,则该文件运行得很好。

C:\app> C:\app\Process.bat C:\app\files\uploads c:\app\files file2 <- WORKS

So there are just 3 input parameters. 因此,只有3个输入参数。

C:\app\files\uploads : the folder location of input files 
c:\app\files         : the output folder
file2                : output file name

If I run the batch file from C:\\app folder I see the output file I want to automate the process from a console app which will be scheduled job But running in visual studio debug mode or clicking the exe file does nothing. 如果我从C:\\ app文件夹运行批处理文件,我会看到输出文件,我想从将要安排作业的控制台应用程序自动执行该过程,但是在Visual Studio调试模式下运行或单击exe文件不会执行任何操作。 I don't get any kind of exception either. 我也没有任何例外。

What can be wrong - permission or other thing I am doing wrong? 有什么可能是错的-许可或我做错的其他事情?

This is the C# code 这是C#代码

static void Main(string[] args)
        {
            RunBatchFile(@"C:\app\Process.bat", @"C:\app\files\uploads c:\app\files 123456");
        }

public static string RunBatchFile(string fullPathToBatch, string args)
        {            
            using (var proc = new Process
            {
                StartInfo =
                {
                    Arguments = args,                    
                    FileName = fullPathToBatch,

                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = false,
                    RedirectStandardError = false
                }
            })
            {
                try
                {
                    proc.Start();
                }
                catch (Win32Exception e)
                {
                    if (e.NativeErrorCode == 2)
                    {
                        return "File not found exception";
                    }
                    else if (e.NativeErrorCode == 5)
                    {
                        return "Access Denied Exception";
                    }
                }
            }

            return "OK";
        }

2 Issues here: 这里有2个问题:

First problem is you need to execute cmd.exe not the batch file. 第一个问题是您需要执行cmd.exe而不是批处理文件。
Second, you are executing it but you need to wait for the process to complete. 其次,您正在执行它,但是您需要等待该过程完成。 Your app is the parent process and because you're not waiting the child process doesn't complete. 您的应用是父进程,并且因为您没有等待子进程无法完成。

You need to issue a WaitForExit(): 您需要发出WaitForExit():

var process = Process.Start(...);
process.WaitForExit();

Here is what you want to do: 这是您要执行的操作:

static void ExecuteCommand(string command)
{
    int exitCode;
    ProcessStartInfo processInfo;
    Process process;

    processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = false;
    process = Process.Start(processInfo);
    process.WaitForExit();
    exitCode = process.ExitCode;
    process.Close();
}

To run your batch file do this from the main(): 要运行您的批处理文件,请从main()中执行以下操作:

ExecuteCommand("C:\app\Process.bat C:\app\files\uploads c:\app\files file2");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM