I'm trying to pipe information between excel and another console for learning.
I have a simple pipe example that works between 2 separate C# console programs. I'm now trying to get it to work with one end being Excel, and another being just a regular console application. I am fairly new to Named Pipes.
For the set up in Excel, a form will appear when a ribbon button is clicked and then a console will appear when a button on that form is clicked. When this console appears, there is no output on the console which is problem one. I've looked at process.StartInfo.RedirectStandardOutput = true;
but I don't think that is the answer since its not working. I feel like this is an easy fix.
I haven't found any documentation on trying to pipe between applications like excel. I've only found simple examples on piping between 2 simple console programs
The second issue I'm having is the error: pipe is broken
after the first message is sent.
Exception thrown: 'System.IO.IOException' in System.Core.dll
An unhandled exception of type 'System.IO.IOException' occurred in System.Core.dll
Additional information: Pipe is broken.
Any and all help/direction is appreciated.
ExcelForm
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPipeA_Click(object sender, EventArgs e)
{
using (Process process = new Process())
{
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
//Client
var client = new NamedPipeClientStream("PipesOfPiece");
if (client.IsConnected == false)
{
Console.WriteLine("Waiting to connect to Server...");
}
client.Connect();
StreamReader reader = new StreamReader(client);
StreamWriter writer = new StreamWriter(client);
Console.Write("Found a sever to connect to. Send a greeting to the server: ");
while (true)
{
string input = Console.ReadLine();
if (String.IsNullOrEmpty(input)) break;
writer.WriteLine(input);
writer.Flush();
Console.WriteLine("PipeB said: " + reader.ReadLine());
}
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
ConsoleAppPipe
class Program
{
static void Main(string[] args)
{
StartServer();
}
static void StartServer()
{
/*Task.Factory.StartNew(() =>
{*/
var server = new NamedPipeServerStream("PipesOfPiece");
if (server.IsConnected == false)
{
Console.WriteLine("Currently waiting for a client to connect...");
}
server.WaitForConnection();
StreamReader reader = new StreamReader(server);
StreamWriter writer = new StreamWriter(server);
Console.Write("A client has connected, awaiting greeting from client... \n");
while (true)
{
var line = reader.ReadLine();
Console.WriteLine("PipeA said: " + line);
writer.WriteLine(Console.ReadLine());
writer.Flush();
}
}
}
This is what shows when I run the the Excel code:
This is what shows when I run the console application code:
-----------------------Update--------------------
button update
private void btnPipeA_Click(object sender, EventArgs e)
{
Program.Main();
}
program
namespace TestNamedPIpeA
{
public class Program
{
public static void Main(/*string[] args*/)
{
//Client
var client = new NamedPipeClientStream("PipesOfPiece");
if (client.IsConnected == false)
{
Console.WriteLine("Waiting to connect to Server...");
}
client.Connect();
StreamReader reader = new StreamReader(client);
StreamWriter writer = new StreamWriter(client);
Console.Write("Found a sever to connect to. Send a greeting to the server: ");
while (true)
{
string input = Console.ReadLine();
if (String.IsNullOrEmpty(input)) break;
writer.WriteLine(input);
writer.Flush();
Console.WriteLine("PipeB said: " + reader.ReadLine());
}
}
}
}