C#Git命令问题

C#Git命令问题,c#,.net,git,C#,.net,Git,我正在开发一个应用程序,需要从C#运行Git命令。我使用这个过程来运行命令,若我正在传递用户名和密码,那个么就是说用户名或密码不正确,但它实际工作正常。下面是我的代码:- public static void PullCode(string gitCommand) { string strPassword = "Password"; ProcessStartInfo gitInfo = new ProcessStartInfo(); Process gitProcess

我正在开发一个应用程序,需要从C#运行Git命令。我使用这个过程来运行命令,若我正在传递用户名和密码,那个么就是说用户名或密码不正确,但它实际工作正常。下面是我的代码:-

public static void PullCode(string gitCommand)
{
    string strPassword = "Password";
    ProcessStartInfo gitInfo = new ProcessStartInfo();
    Process gitProcess = new Process();

    gitInfo.CreateNoWindow = true;
    gitInfo.UserName = "UserNAme";
    gitInfo.Password = Misc.ConvertPassword(strPassword);
    gitInfo.UseShellExecute = false;
    gitInfo.FileName = @"C:\Program Files (x86)\Git\bin\git.exe";  //git repostory directory path
    gitInfo.Arguments = gitCommand; //git command such as "fetch orign"
    gitInfo.WorkingDirectory = @"E:\Code Demo\testrepo";  //YOUR_GIT_REPOSITORY_PATH Where you want to Copy Data
    gitInfo.RedirectStandardError = true;
    gitInfo.RedirectStandardOutput = true;

    using( var proc = new System.Diagnostics.Process() )
    {
        proc.StartInfo = gitInfo;
        proc.Start();

        var output = proc.StandardOutput.ReadToEnd();
        var error = proc.StandardError.ReadToEnd();

        var logRaw = string.IsNullOrEmpty( output ) && !string.IsNullOrEmpty( error )
            ? error.Split( '\n' ).ToArray()
            : output.Split( '\n' ).ToArray();

        proc.WaitForExit();
        proc.Close();
    }
}

你有两个选择。第一个是为git repo(或全局)设置配置,以使用git askpass开关“git config core.askpass git gui--askpass”。将其作为流程的第一个命令调用。这里的缺点是,每次修改命令时,它都会要求输入密码

或者,您可以拥有自己的可执行文件,该文件将请求一次密码,并可以选择将密码存储到应用程序中。但在这里,你必须小心,因为如果你没有妥善储存它将是一个安全风险