How to Pass Parameters to Another Process in C#

How to pass parameters to another process in c#


Process p= new Process();
p.StartInfo.FileName = "demo.exe";
p.StartInfo.Arguments = "a b";
p.Start();

or

Process.Start("demo.exe", "a b");

in demo.exe

static void Main (string [] args)
{
Console.WriteLine(args[0]);
Console.WriteLine(args[1]);
}

You asked how to save these params. You can create new class with static properties and save these params there.

class ParamHolder
{
public static string[] Params { get; set;}
}

and in main

static void Main (string [] args)
{
ParamHolder.Params = args;
}

to get params in any place of your program use:

Console.WriteLine(ConsoleParamHolder.Params[0]);
Console.WriteLine(ConsoleParamHolder.Params[1]);

etc.

pass object from one process to another

I found two good solutions to the above question.

1) Construct a datatable with all given input as column and write that as xml. And start a process with that file as input and read that xml file into the invoked process.
In this solution, I have no control over my new process and I can’t even debug it. I found lot of complexity while doing this.

2) So found this solution, add the exe as a reference to solution and simply invoke it with file as input as simple call to an assembly. Now have total control on it. I can even do exception handling.

Thanks everyone for reply.

how to pass argument to a process

To get the arguments passed you can either use the string[] args in your Main, or you can use Environment.GetCommandLineArgs.

Example:

Console.WriteLine(args[0]);

or

Console.WriteLine(Environment.GetCommandLineArgs[0]);

Using C#, how can I pass an argument from one project to another

If you know which class in MainApp contains the Main method (and that class is public), you can call it from your test project and pass arguments into it just like how you would call any other static method.

The Main method in your test assembly should look like:

public static void Main() {
var arguments = new string[] {"arg1", "arg2"};
MainApp.GetData.Main(arguments);
}

How pass parameters to a dynamic function after a Windows process exits?

You can add an additional Action parameter to your method - an Action is a variable that represents a method with no return type. So if you change your declaration to:

public static void RunCommand(string path, string parms, string completion, 
Action<object, EventArgs, string> exitedCallback)
{
Process myProc = new Process();

myProc.StartInfo.FileName = path;
myProc.StartInfo.Arguments = parms;
myProc.EnableRaisingEvents = true;
myProc.StartInfo.UseShellExecute = false;
myProc.Exited += (o, e) => exitedCallback(o, e, completion);
myProc.Start();
}

So now, even at runtime, you can pass any method that matches the signature:

RunCommand("1.exe", string.Empty, string.Empty, CommandExecuted);
RunCommand("1.exe", string.Empty, string.Empty, (o, e, c) => Console.WriteLine("Foo"));

Calling a console from another and passing parameters in string [] args?

Pretty much what you said:

http://msdn.microsoft.com/en-CA/library/h6ak8zt5.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

Process.Start Method (String, String)
Starts a process resource by specifying the name of an application and
a set of command-line arguments, and associates the resource with a
new Process component.

Passing argument to another instance of the same application

One of the simplest ways to communicate would be to send a Windows message from the second instance to the first instance. The second instance would use PostMessage to send the message and the first instance would override the WndProc method of Form1 to handle the message.

There is an example here:
Send message to a Windows process (not its main window)

That question also has an answer that uses a pipe (and WCF) for the communication.

There are many forms of interprocess communication. Which one you use depends on your needs, for example Windows Messages can't carry a great deal of information whereas a pipe can stream data at very high speed.

Update
A simple alternative could be IPCChannel . This allows you to create an object in the first instance that can be called by the second instance. You can create a method on that object and pass your data as arguments

How to pass three parameters to other demo.exe using C# windows application?

Try this:

System.Diagnostics.Process.Start("demo.exe","arguments");


Related Topics



Leave a reply



Submit