在我的应用程序中,我使用Windows窗体创建了一个GUI,其中有一个带有值的ListBox和一个名为sendto的按钮.用户从列表框中选择并单击sendto按钮.单击此按钮后,应在控制台应用程序上显示从列表框选择的值.在此,以Windows形...
在我的应用程序中,我使用Windows窗体创建了一个GUI,其中有一个带有值的ListBox和一个名为sendto的按钮.用户从列表框中选择并单击sendto按钮.单击此按钮后,应在控制台应用程序上显示从列表框选择的值.在此,以Windows形式开发的GUI充当服务器,而控制台应用程序充当客户端.如何将数据从Windows表单发送到C#中的控制台应用程序?我是C#的新手.
解决方法:
我在回答您的问题:使用C#进行套接字编程…但是一些不了解您的人关闭了您的问题…
我知道您可能是新程序员.但是我发现您善于提出问题,以使自己发展成为一个更好的程序员.我将投票给你! :D
请参阅以下代码,它将帮助您体验小型客户端服务器应用程序.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Aspects;
using System.Diagnostics;
using System.IO;
namespace TestCode
{
public class Program
{
public static StreamReader ServerReader;
public static StreamWriter ServerWriter;
static void Main(string[] args)
{
// here are all information to start your mini server
ProcessStartInfo startServerInformation = new ProcessStartInfo(@"c:\path\to\serverApp.exe");
// this value put the server process invisible. put it to false in while debuging to see what happen
startServerInformation.CreateNoWindow = true;
// this avoid you problem
startServerInformation.ErrorDialog = false;
// this tells that you whant to get all connections from the server
startServerInformation.RedirectStandardInput = true;
startServerInformation.RedirectStandardOutput = true;
// this tells that you whant to be able to use special caracter that are not define in ASCII like "é" or "?"
startServerInformation.StandardErrorEncoding = Encoding.UTF8;
startServerInformation.StandardOutputEncoding = Encoding.UTF8;
// start the server app here
Process serverProcess = Process.Start(startServerInformation);
// get the control of the output and input connection
Program.ServerReader = serverProcess.StandardOutput;
Program.ServerWriter = serverProcess.StandardInput;
// write information to the server
Program.ServerWriter.WriteLine("Hi server im the client app :D");
// wait the server responce
string serverResponce = Program.ServerReader.ReadLine();
// close the server application if needed
serverProcess.Kill();
}
}
}
请注意,在服务器应用程序中,您可以使用以下方法接收客户端信息:
string clientRequest = Console.ReadLine();
Console.WriteLine("Hi client i'm the server :) !");
沃梦达教程
本文标题为:C#-将数据从Windows应用程序发送到控制台应用程序
猜你喜欢
- 详解WCF服务中的svc文件 2023-05-25
- C# 预处理器指令的用法 2023-07-18
- C#中的属性解析(get、set、value) 2023-07-04
- C# 循环判断会进来几次的实现代码 2022-12-26
- C#把EXCEL数据转换成DataTable 2023-05-30
- unity scrollRect实现按页码翻页效果 2023-02-16
- C#关键字之覆写overwrite介绍 2023-05-31
- C# 在PDF文档中创建表格的实现方法 2022-12-02
- Unity UGUI实现卡片椭圆方向滚动 2023-01-16
- 详细分析c# 运算符重载 2023-03-09