How to make it redirect

0

I made a C # application, in addition to performing the basic functions, I want to be able to run with batch scripts, but the problem is that I run Console.WriteLine and it does not write anything in 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 also heard about Trace.WriteLine , but it did not work either!

    
asked by anonymous 17.12.2015 / 12:46

1 answer

1

As I understand it, you can use the class Process to create a process, and ProcessStartInfo which will specify and set the properties when your process is initialized, in case the values will be for 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 /C , thus calling Windows Explorer, and the window will be hidden, you can substitute for other commands.

Font .

    
21.12.2015 / 18:26