How to make it redirect

Asked

Viewed 106 times

0

I made an application in C#, in addition to performing the basic functions, I want it to be possible to run with scripts in batches, but the problem is that I run Console.WriteLine and he doesn’t write anything on the CMD window.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Programs
{
    static class Program
    {
        static Mutex mutex = new Mutex(true, "TS4");
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                if (args.Length > 0)
                {
                    Application.Run(new Form_Install());
                }
                else
                {
                    Trace.WriteLine("Missing Var packed_filename");
                    Trace.WriteLine("Missing Var packed_game");
                    Trace.WriteLine("Missing Var packed_type");
                    MessageBox.Show("Parâmetros não encontrados.");
                }

                mutex.ReleaseMutex();
                Trace.WriteLine("Mutex foi lançado!");
            }
            else
            {
                MessageBox.Show("Erro ao processar solicitação! O aplicativo já está em execução, termine a outra instalação e tente novamente!" , "O Thread atual já está em uso!" , MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

I’ve also heard of Trace.WriteLine, but it didn’t work either!

  • 3

    It will not write on CMD. This is a Winforms application

  • Ah ta thought that even being from GUI it generates output! But so has to hide the console window?

  • Look even though I have switched to Console Application it does not write anything in cmd

  • It even generates output, but it’s not in CMD. Switching to the App Console probably won’t help. I think the ideal is to create a Console App.

  • Are you using Console.Write after you switched to console? @Nathan1302

  • Yes it worked I switched the Trace for Console and it worked. But how do I hide the CMD window from my app?

  • Do you want to run a cmd command and keep your console application window hidden? If so, I’ve built an example.

Show 2 more comments

1 answer

1

From what I understand, you can use the class Process to create a process and the Processstartinfo that will specify and define the properties when your process is initialized, in which case the values will be for the cmd.exe.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RunCMDCommand
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();

            System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();

            startinfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Olcuta a janela da aplicação.
            startinfo.FileName = "cmd.exe";
            startinfo.Arguments = "/C explorer "; // O "/C" transporta o comando de saida especificado por uma string e conclui.

            process.StartInfo = startinfo;
            process.Start();
        }
    }
}

The program will execute the command explorer as an example, which comes before the /C, so calling Windows Explorer, and the window will be hidden, you can replace with other commands.

Source.

  • I wonder if it redirects for example cmd, but cmd is already open without starting a new process

  • How so redirect cmd? You want to get the output of the commands that are executed in cmd?

  • ex: has an app it has already been opened, I want my program use the Redirectstandardoutput in this program. is possible?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.