Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#程序退出时保持批处理文件运行?_C#_Batch File - Fatal编程技术网

如何在C#程序退出时保持批处理文件运行?

如何在C#程序退出时保持批处理文件运行?,c#,batch-file,C#,Batch File,我已经编写了一个C#代码,可以读取几个.xml文件(包含可执行文件、路径、参数..xml文件包含一系列需要运行的可执行文件),并且将创建几个与.xml文件关联的批处理文件 创建每个批处理文件后,程序将以C#代码(当然使用cmd)执行每个批处理文件。问题是,有些批处理文件会运行数天,而要让C#程序继续运行,这是对内存/瞬间的浪费。 是否可以保持.bat文件运行并关闭C代码?假设我们不需要任何来自C代码的错误报告 下面是执行批处理文件的代码 //Execute BatchFile p

我已经编写了一个C#代码,可以读取几个.xml文件(包含可执行文件、路径、参数..xml文件包含一系列需要运行的可执行文件),并且将创建几个与.xml文件关联的批处理文件

创建每个批处理文件后,程序将以C#代码(当然使用cmd)执行每个批处理文件。问题是,有些批处理文件会运行数天,而要让C#程序继续运行,这是对内存/瞬间的浪费。 是否可以保持.bat文件运行并关闭C代码?假设我们不需要任何来自C代码的错误报告

下面是执行批处理文件的代码

    //Execute BatchFile
    public static void ExecuteCommand(string command, string dummyPath)
    {
        int exitCode;
        ProcessStartInfo processInfo;
        Process process;

        processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
        processInfo.WorkingDirectory = dummyPath;
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;

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

        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();

        exitCode = process.ExitCode;

        Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
        Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
        Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
        process.Close();
    } 

cmd批处理进程属于您的程序进程,主进程停止,子进程将被终止


我认为您可以尝试在程序中创建windows任务计划,在taski中运行批处理,程序停止,任务计划仍将运行。

cmd批处理进程属于您的程序进程,主进程停止和子进程将被终止


我认为您可以尝试在程序中创建windows任务计划,在taski中运行批处理,程序停止,任务计划仍将运行。

cmd批处理进程属于您的程序进程,主进程停止和子进程将被终止


我认为您可以尝试在程序中创建windows任务计划,在taski中运行批处理,程序停止,任务计划仍将运行。

cmd批处理进程属于您的程序进程,主进程停止和子进程将被终止


我认为您可以尝试在程序中创建windows任务计划,在taski中运行批处理,程序停止,任务计划仍将运行。

如果我理解正确,我认为如果您不需要
.bat
文件的任何反馈,这是可能的。 我在自己的机器上测试了这行代码,效果很好

    static void Main(string[] args)
    {
        System.Diagnostics.Process.Start(@"d:\simple.bat");
    }

出于测试目的,我只需将
PING www.google.com
放在我的
.bat
文件中。

如果我理解正确,我认为如果您不需要
.bat
文件的任何反馈,这是可能的。 我在自己的机器上测试了这行代码,效果很好

    static void Main(string[] args)
    {
        System.Diagnostics.Process.Start(@"d:\simple.bat");
    }

出于测试目的,我只需将
PING www.google.com
放在我的
.bat
文件中。

如果我理解正确,我认为如果您不需要
.bat
文件的任何反馈,这是可能的。 我在自己的机器上测试了这行代码,效果很好

    static void Main(string[] args)
    {
        System.Diagnostics.Process.Start(@"d:\simple.bat");
    }

出于测试目的,我只需将
PING www.google.com
放在我的
.bat
文件中。

如果我理解正确,我认为如果您不需要
.bat
文件的任何反馈,这是可能的。 我在自己的机器上测试了这行代码,效果很好

    static void Main(string[] args)
    {
        System.Diagnostics.Process.Start(@"d:\simple.bat");
    }

出于测试目的,我只需将
PING www.google.com
放在我的
.bat
文件中。

谢谢你的帮助。这很有魅力。我把自己的代码复杂化是愚蠢的。答案回到了简单。再次感谢。'var psi=new ProcessStartInfo(@BatchPath);psi.WorkingDirectory=@DummyFilePath;进程启动(psi);'指定cmd的工作目录。感谢您的帮助。这很有魅力。我把自己的代码复杂化是愚蠢的。答案回到了简单。再次感谢。'var psi=new ProcessStartInfo(@BatchPath);psi.WorkingDirectory=@DummyFilePath;进程启动(psi);'指定cmd的工作目录。感谢您的帮助。这很有魅力。我把自己的代码复杂化是愚蠢的。答案回到了简单。再次感谢。'var psi=new ProcessStartInfo(@BatchPath);psi.WorkingDirectory=@DummyFilePath;进程启动(psi);'指定cmd的工作目录。感谢您的帮助。这很有魅力。我把自己的代码复杂化是愚蠢的。答案回到了简单。再次感谢。'var psi=new ProcessStartInfo(@BatchPath);psi.WorkingDirectory=@DummyFilePath;进程启动(psi);'指定cmd的工作目录。