c# - 如何将用户输入数据从控制台程序获取到C#程序?

标签 c# c

如何让用户在控制台应用程序(测试应用程序)中向 C# 程序(监控应用程序)输入输入内容?我已成功从硬编码在测试应用程序中的控制台向我的监视器应用程序获取了一个字符串值。现在,我想允许用户从测试应用程序中的控制台输入字符串,并且我想从受监控的应用程序捕获用户在测试应用程序中输入的字符串。

以下是测试应用程序和监控应用程序代码。

测试应用

 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();
        }
    }
}

监控应用代码

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();
                }
            }
        }
    }
}

最佳答案

创建一个函数来检索 C 程序所需的值。

使用 DLLImport 从 C# 程序中调用该函数: https://msdn.microsoft.com/en-us/library/aa984739(v=vs.71).aspx

关于c# - 如何将用户输入数据从控制台程序获取到C#程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41220603/

相关文章:

c - 二维数组的空单元格的值是多少?

c# - 如何从流中读取 Access 文件 (.accdb)?

c# - 使用异步时未处理 SqlConnection

c# - JavaScript 验证和 jQuery 验证有什么区别?

在C中创建链接列表

c - 我们应该使用多个接受器套接字来接受大量连接吗?

c# - Silverlight 带水印的自动完成框

c# - UIA Automation 元素 GDI Target 应用程序过程中的泄漏

c - 如何修复 Command/Developer/usr/bin/clang 失败,退出代码为 1?

c - 在 C 中,动态分配的字符串文字的间接操作是如何真正工作的?