Core打开外部终端

Core打开外部终端

本文介绍了.Net Core打开外部终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将批处理脚本迁移到.Net core,并且试图从当前终端打开另一个终端并运行命令(我不需要stderr o stout)。

I'm migrating batch script to .Net core and I'm trying to open another terminal from current terminal and run a command (I don't need stderr o stout).

使用批处理仅需要以下命令: start cmd / K gulp 。我正在尝试对.Net核心执行相同操作,但只找到了在当前终端中运行命令的方法。

With batch only needs this command: start cmd /K gulp. I'm trying to do the same with .Net core but only found the way to run the command inside current terminal.

private static string Run (){
    var result = "";
    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = $"/c \"gulp browserSync\"";
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;

        using (Process process = Process.Start(startInfo))
        {
            result = process.StandardError.ReadToEnd();
            process.WaitForExit();
        }
    }
    catch (Exception Ex)
    {
        Console.WriteLine(Ex.Message);
        Console.ReadKey();
    }
    return result;
}

我正在尝试更改此属性以便在另一个终端中打开:

I'm trying changing this properties in order to open in another terminal:

startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.UseShellExecute = true;

但请例外:


推荐答案

感谢@我弯弯曲曲的答案,我找到了一种解决方法。由于安全性的限制,需要用户/密码凭证才能使当前终端自动打开新终端。

Thanks to @bender-bending answer I found a way to solve it. Due security limitations need user/password credentials in order to autorice current terminal to open a new one.

WorkingDirectory,用户,密码和域是必需的。
不创建任何窗口,重定向输出和重定向错误必须为false,以便在新窗口中查看命令结果。

WorkingDirectory, user, password and domain are required.
Create no window, redirect output and redirect error must be false, in order to see command result in new window.

public static void Sample(){
    try
    {
        Console.Write("Password: ");
        StringBuilder password = new StringBuilder();
        while (true)
        {
            var key = System.Console.ReadKey(true);
            if (key.Key == ConsoleKey.Enter) break;
            password.Append(key.KeyChar);
        }

        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            WorkingDirectory = "C:/path_to/Gulp",
            Arguments = $"/c \"gulp browserSync\"",
            UseShellExecute = false,
            RedirectStandardOutput = false,
            RedirectStandardError = false,
            UserName = Machine.User(),
            PasswordInClearText = password.ToString(),
            Domain = Machine.Domain(),
            CreateNoWindow = false
        };

        Process proc = new Process();
        proc.StartInfo = startInfo;
        proc.Start();
        //proc.WaitForExit();
    } catch (Exception ex)
    {
        System.Console.WriteLine(ex);
        System.Console.ReadKey();
    }
}

.Net Core没有获取方法用户和域。我们可以使用此类从环境变量获取此值。

.Net Core doesn't have a method to obtain user and domain. We can use this class to get this values from environment variables.

public static class Machine
{
    public static string User(){
        return Environment.GetEnvironmentVariable("USERNAME") ?? Environment.GetEnvironmentVariable("USER");
    }

    public static string Domain(){
        return Environment.GetEnvironmentVariable("USERDOMAIN") ?? Environment.GetEnvironmentVariable("HOSTNAME");
    }
}

希望它会有所帮助!

这篇关于.Net Core打开外部终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:47