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
Using C# .Net Libraries to Check for Imap Messages from Gmail Servers
How to Assign a Func<> Conditionally Between Lambdas Using the Conditional Ternary Operator
How to Use a MySQL User Defined Variable in a .Net MySQLcommand
How to Get the Groups of a User in Active Directory? (C#, ASP.NET)
Add Items to Columns in a Wpf Listview
Is It Worthwhile to Initialize the Collection Size of a List<T> If It's Size Reasonably Known
How to Use a Reserved Keyword as an Identifier in My JSON Model Class
Convert Comma Separated String of Ints to Int Array
How to Compare (Directory) Paths in C#
Xml-Selectnodes with Default-Namespace via Xmlnamespacemanager Not Working as Expected
Assembly Binding Redirect: How and Why
Log4Net: Programmatically Specify Multiple Loggers (With Multiple File Appenders)
Why Is the 'This' Keyword Required to Call an Extension Method from Within the Extended Class
Smoothly Connecting Circle Centers
How to List All Processes Running in Windows