Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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代码运行_C#_.net_Batch File - Fatal编程技术网

C# 批处理文件未从c代码运行

C# 批处理文件未从c代码运行,c#,.net,batch-file,C#,.net,Batch File,我在一个共享位置有一个批处理文件,我正在从.net代码执行该文件,但批处理文件实际上没有执行。事实上,批处理文件的整个代码都没有工作,只是部分代码在工作。我假设.net代码执行正在完成并停止批处理文件的执行。 下面是我的.net代码 private static void ExecuteCommand(string command, string arguments) { try { Console.WriteLine("Code

我在一个共享位置有一个批处理文件,我正在从.net代码执行该文件,但批处理文件实际上没有执行。事实上,批处理文件的整个代码都没有工作,只是部分代码在工作。我假设.net代码执行正在完成并停止批处理文件的执行。 下面是我的.net代码

private static void ExecuteCommand(string command, string arguments)
    {
        try
        {
            Console.WriteLine("Code execution starts");
            Console.WriteLine("command: " + command);
            Console.WriteLine("arguments: " + arguments); 
            var process = new Process();

            process.StartInfo.FileName = command;
            if (!string.IsNullOrEmpty(arguments))
            {
                process.StartInfo.Arguments = arguments;
            }

            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //process.StartInfo.CreateNoWindow = false;
            //process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            process.StartInfo.UseShellExecute = true;
            process.EnableRaisingEvents = true;
            //process.StartInfo.RedirectStandardError = true;
            //process.StartInfo.RedirectStandardOutput = true;
            string stdError;
           // process.Start();
            Thread.Sleep(60000);
            ThreadPool.QueueUserWorkItem(delegate
            {
                process.Start();
               // process.WaitForExit();
            });
            //Thread.Sleep(60000);

            //ThreadStart ths = new ThreadStart(delegate()
            //{
            //    process.Start();
            //    //process.BeginOutputReadLine();
            //    //stdError = process.StandardError.ReadToEnd();
            //});

           //process.Start();

           //process.BeginOutputReadLine();
           //stdError = process.StandardError.ReadToEnd();

            Console.WriteLine("Code execution ends");
        }
        catch (Exception e)
        {
            Console.WriteLine("Code execution error: "+e.Message +"||"+e.InnerException.ToString()); 

        }
    }
要求是开始执行批处理文件,不要等待批处理文件执行的任何输出或完成。当我从命令提示符或windows资源管理器运行批处理文件时,它工作正常。无法在.net代码中使用cmd.exe,因为我们需要在批处理文件不存在或权限问题时处理例外情况

我认为您可以使用WaitForExit方法,而不是使用可能或可能不足的睡眠

更多信息:

如果这不起作用,您可以使用一种变通方法,比如使用在流程开始之前创建的文件,并在流程结束时由批处理删除,然后由FileSystemWatcher定期检查该文件,以检查流程是否完成。

您是否尝试过在process.StartInfo中设置WorkingDirectory?检查批处理文件停止处的执行情况。也许有某种错误。