从一个控制台程序读取用户输入,将它们输入到另一个C#程序. [英] Read user inputs from one console program get them to another c# program.

查看:130
本文介绍了从一个控制台程序读取用户输入,将它们输入到另一个C#程序.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用户在控制台应用程序(测试应用程序)中输入到C#程序(监视应用程序)的输入?我已经从控制台中成功将字符串值添加到我的监视器应用程序中,该值已硬连接到测试应用程序中.现在我要允许用户输入一个字符串 从测试应用程序的控制台中,我想从受监视的应用程序中捕获该用户在测试应用程序中输入的字符串.

How to get a user entered inputs in a console application (test app) to a c# program (monitor app)? I have successfully got a string value to my monitor app from the console that is hard corded in the test app. Now I want to allow the user to input a string from the console in test app and I want to capture that user entered string in test app from my monitored app.

这是测试应用和监控应用的代码.

Here are the test app and monitor app codes.

测试应用程序:

using System;


namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Test2");
            Console.Write("Enter a string ");
            string txtOne = Console.ReadLine();
            Console.WriteLine(txtOne);
            //Console.ReadLine();
        }
    }
}


监视应用程序:


monitor app:

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

namespace GetStandardOutput
{
    class Program
    {
        static void Main()
        {
            //
            // Setup the process with the ProcessStartInfo class.
            //
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = @"C:\Users\erandaka\Documents\Visual Studio 2015\Projects\TestApp\TestApp\bin\Debug\TestApp.exe"; // Specify exe name.
            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            //
            // Start the process.
            //
            using (Process process = Process.Start(start))
            {
                //
                // Read in all the text from the process with the StreamReader.
                //
                using (StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Console.Write(result);
                    Console.ReadLine();
                }
            }
        }
    }
}



推荐答案

如果您需要在进程之间进行通信(IPC ),则需要使用许多可用工具之一.对于.NET,您 可能会考虑使用简单的 NamedPipe (如果它位于同一台计算机上,或者 WCF .如果您想使用托管应用,请考虑使用 网络API .

If you need to communicate between processes (IPC) then you'll want to use one of the many tools available for this. For .NET you might consider using a simple NamedPipe if it is on the same machine or perhaps peer-to-peer for remote machines. If you need to pass something more complicated then take a look at WCF. If you are wanting to use a hosted app then consider using web API.

迈克尔·泰勒
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于从一个控制台程序读取用户输入,将它们输入到另一个C#程序.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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