Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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# PreBuildAction使用命令输入调用exe时发生异常_C#_Visual Studio_Batch File_Pre Build Event - Fatal编程技术网

C# PreBuildAction使用命令输入调用exe时发生异常

C# PreBuildAction使用命令输入调用exe时发生异常,c#,visual-studio,batch-file,pre-build-event,C#,Visual Studio,Batch File,Pre Build Event,我的(visual studio)预构建操作调用可执行文件时遇到问题,这需要控制台输入 代码如下所示: using System; using System.Diagnostics; using System.Text; namespace MyMinumumExample { internal class MinimumExample { private StringBuilder stdOutput = null; private read

我的(visual studio)预构建操作调用可执行文件时遇到问题,这需要控制台输入

代码如下所示:

using System;
using System.Diagnostics;
using System.Text;

namespace MyMinumumExample
{
    internal class MinimumExample
    {
        private StringBuilder stdOutput = null;

        private readonly string assemblyFilePath;
        private readonly Process gitProcess = new Process()
        {
            StartInfo = new ProcessStartInfo()
            {
                FileName = "git",
                RedirectStandardOutput = true,
                UseShellExecute = false,
            },
        };

        public MinimumExample(string path)
        {
            assemblyFilePath = path;
        }

        private static int Main(string[] args)
        {
            string path = args[0];
            var program = new MinimumExample(path);

            if (program.CheckIfFileIsDirty(path))
            {
                Console.WriteLine("Changes will be discarded after compiling. Do you want to proceed?");
                while (true)
                {
                    Console.Write("[y/n]: ");
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.KeyChar == 'y')
                        break;

                    if (key.KeyChar == 'n')
                        return 1;

                    Console.WriteLine();
                }
            }
            return 0;
        }

        private bool CheckIfFileIsDirty(string path)
        {
            string gitCmdArgs = string.Format("diff --shortstat -- {0}", path);

            stdOutput = new StringBuilder();

            gitProcess.StartInfo.Arguments = gitCmdArgs;
            gitProcess.Start();

            gitProcess.BeginOutputReadLine();
            gitProcess.OutputDataReceived += GitProcessOutputHandler;

            gitProcess.WaitForExit();
            gitProcess.OutputDataReceived -= GitProcessOutputHandler;

            if (gitProcess.ExitCode != 0) throw new Exception(string.Format("Process 'git {0}' failed with code {1}", gitCmdArgs, gitProcess.ExitCode));

            return !string.IsNullOrWhiteSpace(stdOutput.ToString());
        }


        private void GitProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!string.IsNullOrWhiteSpace(outLine.Data))
            {
                stdOutput.Append(outLine.Data);
            }
        }
    }
}
call $(SolutionDir)MinimumExample.exe $(ProjectDir)dirtyExampleFile.cs
IF %ERRORLEVEL% GTR 0 (
EXIT 1
)
如果我从cmd shell或批处理文件运行此命令,则一切正常。如果我把它放到预构建操作中,我会在第38行得到一个验证:

ConsoleKeyInfo key = Console.ReadKey();
预构建操作如下所示:

using System;
using System.Diagnostics;
using System.Text;

namespace MyMinumumExample
{
    internal class MinimumExample
    {
        private StringBuilder stdOutput = null;

        private readonly string assemblyFilePath;
        private readonly Process gitProcess = new Process()
        {
            StartInfo = new ProcessStartInfo()
            {
                FileName = "git",
                RedirectStandardOutput = true,
                UseShellExecute = false,
            },
        };

        public MinimumExample(string path)
        {
            assemblyFilePath = path;
        }

        private static int Main(string[] args)
        {
            string path = args[0];
            var program = new MinimumExample(path);

            if (program.CheckIfFileIsDirty(path))
            {
                Console.WriteLine("Changes will be discarded after compiling. Do you want to proceed?");
                while (true)
                {
                    Console.Write("[y/n]: ");
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.KeyChar == 'y')
                        break;

                    if (key.KeyChar == 'n')
                        return 1;

                    Console.WriteLine();
                }
            }
            return 0;
        }

        private bool CheckIfFileIsDirty(string path)
        {
            string gitCmdArgs = string.Format("diff --shortstat -- {0}", path);

            stdOutput = new StringBuilder();

            gitProcess.StartInfo.Arguments = gitCmdArgs;
            gitProcess.Start();

            gitProcess.BeginOutputReadLine();
            gitProcess.OutputDataReceived += GitProcessOutputHandler;

            gitProcess.WaitForExit();
            gitProcess.OutputDataReceived -= GitProcessOutputHandler;

            if (gitProcess.ExitCode != 0) throw new Exception(string.Format("Process 'git {0}' failed with code {1}", gitCmdArgs, gitProcess.ExitCode));

            return !string.IsNullOrWhiteSpace(stdOutput.ToString());
        }


        private void GitProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!string.IsNullOrWhiteSpace(outLine.Data))
            {
                stdOutput.Append(outLine.Data);
            }
        }
    }
}
call $(SolutionDir)MinimumExample.exe $(ProjectDir)dirtyExampleFile.cs
IF %ERRORLEVEL% GTR 0 (
EXIT 1
)
如果我使用
start
调用可执行文件,那么示例可以运行,但是我没有得到exe的退出代码(但可能是新的cmd shell的退出代码)

或者,我可以获取cmdshell的退出代码来验证可执行文件是否干净地退出,或者我找到了从构建事件控制台读取键盘输入的方法

有没有人有办法解决这个问题


最好的问候和感谢提前

我找到了获取退出代码的解决方案: 使用带有
/wait
选项的
start
解决了我的问题

PreBuildEvent现在是:

start "Update version of $(ProjectName)" /wait $(SolutionDir)MinimumExample.exe $(ProjectDir)dirtyExampleFile.cs


我找到了获取退出代码的解决方案: 使用带有
/wait
选项的
start
解决了我的问题

PreBuildEvent现在是:

start "Update version of $(ProjectName)" /wait $(SolutionDir)MinimumExample.exe $(ProjectDir)dirtyExampleFile.cs


在生成操作中的可执行文件之前,您是否尝试过使用
echo y |
?它模拟键入
Console.ReadKey()的
y
在生成操作中的可执行文件之前,您是否尝试过使用
echo y |
?它模拟键入
Console.ReadKey()的
y