[RESOLVED] Pass Values from one app to another
I am in the process of creating a program that needs to pass some information to another program. The program that needs to receive the data is meant as an update program to the first. I send a new copy of the host program over the sockets connection, then I need to pass the file name and location to the update program. After this information is sent the host program will quit so that the update can overwrite the host with the new version. The the update program restarts the host and everything goes on like normal. I have everything all-ready in place, except for passing the file information. I could just save the information in a text file and read form this, but it seems messy and illogical. Any advice?
-Adam
Re: Pass Values from one app to another
Does your main application launch the update application? I would suggest you use command line paramters to send the data you want to send, over to the updater app when it is launched. The updater app would then read in those values from its commandline param collection, and know what files it needs to work with.
Re: Pass Values from one app to another
Yes, actually. Both application are command like apps. The host program launches the update program. So command line parameters? Could you recommend any articles so I may learn about them? Thanks for the advice.
-Adam
Re: Pass Values from one app to another
What do you mean by "command like apps"? Does that mean they are console applications and not Windows Forms applications? Or something else?
You can use Environment.CommandLine to get any command line params that were passed to the given application. It gives you one big string of all of them though. As an alternative, you can use My.Application.CommandLineArgs to get a pre-parsed array (parsed at spaces) of the commandline args.
The way you PASS command line args is simply by specifying them after the exe name.
For example, in Windows, you can go to start -> run, and type
iexplore.exe www.vbforums.com
and it will launch internet explorer, and navigate to www.vbforums.com
That is an example of a command line parameter. Of course it is up to the program to read in these params and take appropriate action as to what should happen.
To launch your update exe, it is easy to use the process class, and pass the command line args with that.
There are probably plenty of articles on how to use it, and I would suggest reading over the actual MSDN doc on the ProcessStartInfo, the sample code gives a decent example.
http://msdn.microsoft.com/en-us/libr...startinfo.aspx
Re: Pass Values from one app to another
Sorry. They both are console apps. I got my OS mixed up. LOL. Thanks for your help. I will try the command like args thing.
-Adam
Re: Pass Values from one app to another
Ive tried these 2 different ways I tried to call the program with the arguments. I am have not been able to receive anything in the other program.
VB.NET Code:
System.Diagnostics.Process.Start("C:\Documents and Settings\Adam\My Documents\My Dropbox\Work\Robot Updater\Robot Updater\bin\Debug\Robot Updater.exe, "Test Test Test Test")
Process.Start("C:\Documents and Settings\Adam\My Documents\My Dropbox\Work\Robot Updater\Robot Updater\bin\Debug\Robot Updater.exe", "Test Test Test Test")
VB.NET Code:
Dim inputArgument As String = "/input="
Dim inputName As String = ""
For Each s As String In My.Application.CommandLineArgs
If s.ToLower.StartsWith(inputArgument) Then
inputName = s.Remove(0, inputArgument.Length)
End If
Next
If inputName = "" Then
Console.WriteLine("No input name")
Else
Console.WriteLine("Input name: " & inputName)
End If
I tried that code on the recieving side. It is stright from msdn. It always displays "No Input Name".
I have also been looking and could not fins anything that would pass information between 2 programs.
-Adam
Re: Pass Values from one app to another
I was able to get it to work with the following code:
On the host side :
VB.NET Code:
Process.Start("C:\Documents and Settings\Adam\My Documents\My Dropbox\Work\Robot Updater\Robot Updater\bin\Debug\Robot Updater.exe", TextBox1.Text.ToString)
Which just for testing, It takes the value of textbox1 and passes it to Robot Updater.exe as an argument when it launches.
On the updater side:
VB.NET Code:
Dim strAllArgs(My.Application.CommandLineArgs.Count - 1) As String
Dim x As Integer = 0
For Each arg As String In My.Application.CommandLineArgs
Try
strAllArgs(x) = arg
Console.WriteLine(strAllArgs(x))
Catch ex As Exception
strAllArgs(x) = "Could not write argument."
Console.WriteLine(ex.Message)
End Try
x += 1
Next
If My.Application.CommandLineArgs.Count = 0 Then
Console.WriteLine("No Arguments")
End If
That code receives all the arguments and puts them on separate lines. It is using a space as a delimiter right now, so each word ends on its own line.
Thanks Kleinma for pointing me in the right direction.
-Adam