.Net Core 打开外部终端 [英] .Net Core open external terminal

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

问题描述

我正在将批处理脚本迁移到 .Net 核心,我正在尝试从当前终端打开另一个终端并运行命令(我不需要 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 core 做同样的事情,但只找到了在当前终端内运行命令的方法.

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;

但有一个例外:

UseShellExecute 必须始终设置为 false.

UseShellExecute must always be set to false.

推荐答案

感谢@bender-bending 的回答,我找到了解决它的方法.由于安全限制,需要用户/密码凭据才能自动启动当前终端以打开新终端.

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.

需要工作目录、用户、密码和域.
不创建窗口,重定向输出和重定向错误必须为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 打开外部终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆