Update (потому что я missunderstood вопрос):
Вы можете использовать класс процесса, чтобы запустить процесс из приложения Forms и просто перенаправить поток вывода.
Вы бы выполнить это следующим образом:
ExecuteProcess(@"ConsoleApp.exe", "some arguments here");
// and now you can access the received data from the process from the
// receivedData variable.
Код:
/// <summary>
/// Contains the received data.
/// </summary>
private string receivedData = string.Empty;
/// <summary>
/// Starts a process, passes some arguments to it and retrieves the output written.
/// </summary>
/// <param name="filename">The filename of the executable to be started.</param>
/// <param name="arguments">The arguments to be passed to the executable.</param>
private void ExecuteProcess(string filename, string arguments)
{
Process p = new Process();
// Define the startinfo parameters like redirecting the output.
p.StartInfo = new ProcessStartInfo(filename, arguments);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.OutputDataReceived += this.OutputDataReceived;
// Start the process and also start reading received output.
p.Start();
p.BeginOutputReadLine();
// Wait until the process exits.
p.WaitForExit();
}
/// <summary>
/// Is called every time output data has been received.
/// </summary>
/// <param name="sender">The sender of this callback method.</param>
/// <param name="e">The DataReceivedEventArgs that contain the received data.</param>
private void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.receivedData += e.Data;
}
Старый Ответ:
В принципе, вы получите доступ ко всем указанным аргументов командной строки, обращаясь к «арг "в основном методе. Маленький пример печатает все указанные аргументы командной строки (они переходят с символом пробела между ними) на консоль и ждет нажатия клавиши перед выходом.
Пример:
/// <summary>
/// Represents our program class which contains the entry point of our application.
/// </summary>
public class Program
{
/// <summary>
/// Represents the entry point of our application.
/// </summary>
/// <param name="args">Possibly spcified command line arguments.</param>
public static void Main(string[] args)
{
// Print the number of arguments specified to the console.
Console.WriteLine("There ha{0} been {1} command line argument{2} specified.",
(args.Length > 1 ? "ve" : "s"),
args.Length,
(args.Length > 1 ? "s" : string.Empty));
// Iterate trough all specified command line arguments
// and print them to the console.
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("Argument at index {0} is: {1}", i, args[i]);
}
// Wait for any key before exiting.
Console.ReadKey();
}
}
не смущайтесь аргументами в первом заявлении WriteLine, я просто хотел напечатать единственный и множественное число правильно.
Вы можете указать аргумент командной строки либо путем передачи их в командной строке:
Пример: Your.exe argument1 argument2 argument3
Или в IDE, используя свойства проекта (справа нажмите на ваш проект в обозревателе решений -> Свойства -> Debug -> аргументы командной строки)
Я надеюсь, что я правильно понял ваш вопрос, и это помогает ;-)
вы можете передать вход и выход в качестве аргумента командной строки в качестве строки [] args – VladL
@ VladL Я не понимаю, можете ли вы расширить пожалуйста? –
Вы хотите разделить свой код? Я имею в виду, создать консоль в другом процессе, чем приложение форм? Или вам нравится «AllocConsole» в вашем приложении приложения форм? – Maximus