如何将我的变量值从控制台应用程序传递到Windows窗体应用程序

时间:2015-09-27 09:22:08

标签: c#

在计算控制台应用程序中的数据后,我需要将其传递给Windows窗体应用程序,然后在条形图表上显示这些值,我就陷入了我的项目。

1 个答案:

答案 0 :(得分:0)

您有各种各样的选择,包括各种运输方式,复杂性,性能和高架。其中一个简单的:

说你有服务:

public class ServiceType
{
    public void DoSomething(string someData)
    {
      Debug.WriteLine("Got this " + someData);
    }
}

然后在托管应用程序(Windows窗体)中:

      IDictionary properties = new Hashtable();
      properties.Add("authorizedGroup", "NT AUTHORITY\\NETWORK SERVICE"); // or some other user account that runs the console app
      properties.Add("portName", "myApp" + Guid.NewGuid()); // if you rapidly restart your app then the pipe may still be there
      IpcServerChannel serverChannel = new IpcServerChannel(properties, null);
      ChannelServices.RegisterChannel(serverChannel, true);
      RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceType), "service", WellKnownObjectMode.Singleton);

然后在控制台应用中:

          var pipename = System.IO.Directory.GetFiles(@"\\.\pipe\").FirstOrDefault(x => x.Contains("myApp")))

          IpcClientChannel clientChannel = new IpcClientChannel();
          ChannelServices.RegisterChannel(clientChannel, true);
          var s = (ServiceType)Activator.GetObject(typeof (ServiceType), "ipc://" + pipename.Replace(@"\\.\pipe\", string.Empty) + "/service");
          s.DoSomething("data");
          ChannelServices.UnregisterChannel(clientChannel);

当然,您必须共享ServiceType的库。