views:

28

answers:

2

i have 2 vb applications. this is the code for the first one which when a button is clicked it will check if the other application is already open. If not, it'll open that application -

Dim sComputer As String
    sComputer = Environ("COMPUTERNAME")
    Dim LocalByName As Process() = Process.GetProcessesByName("ticket.prices", sComputer)
    If LocalByName.Length = 0 Then
        System.Diagnostics.Process.Start("http://ticket.prices.application")
    End If

this runs fine. but what i need is that the customerid on the application 1 that is calling application 2, should be transfered while opening app 2. e.g - Customer 10001 screen is open on app 1. When i click open app 2, the above code runs and opens app 2. how do i make app 2 open to customer 10001 screen. Is there any way to pass parameters while opening app 2 in System.Diagnostics.Process.Start ?

+2  A: 

Use the version of 'Process.Start' that takes 2 strings, the second being the commandline parameters. See here for details.

ho1
+1  A: 

You want the ProcessStartInfo class, or use the Start method taking to strings. ProcessStartInfo gives you a lot of options about how to start your program, which often comes in handy. Its good to get familiar with it.

Dim info as New ProcessStartInfo()
info.Arguments = "10001"
info.FileName = "exename"
Dim LocalByName as New Process()
LocalByName.StartInfo = info
LocalByName.Start()

Getting the arguments in the new program is accomplished via Environment.GetCommandLineArgs()

For Each arg As String In Environment.GetCommandLineArgs()
    Console.WriteLine(arg)
Next arg

It looks like what you ultimately want to accomplish is getting the currently selected row from App 1 and passing that to the second program, though. Is this correct? That opens a whole new ball of wax involving interprocess communication.

EDIT: The simplest way to get the selected edit would be to write the id out to a text file. You have to be careful when doing this because if you just write System.IO.File.WriteAllText("selectedrow.txt", "123"), you'll write to the app's startup path directory. You'll want to get the exe's current path as below

Dim u as New Uri(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
dim exepath as String = System.IO.Path.GetDirectoryName(u.LocalPath)
dim fullPath as String = System.IO.Path.Combine(exepath, "selectedrow.txt")
System.IO.File.WriteAllText(fullpath, "123")

This will overwrite the text in the file every time you change rows. You want to wrap this in a try/catch block so as not to crash the program. Make sure you log the errors; don't just swallow them. To read the data, you just do

dim id as string = System.IO.File.ReadAllText(PathToFileYoureWritingToInTheOtherProgram)

in the other program.

This isn't necessarily the best way to go about things, but its the simplest way I know of off the top of my head.

You might could look at MessageQueues if you a better solution, but as long as you're not changing selected rows every 100ms, writing the file should work fine.

Tim Coker
yes thats what i want. Is it a wrong way of doing this?
refer
Its not wrong, but it can complicate things considerably. The simplest thing to do would be write the currently selected id out to a file whenever you change selection in your app, which can be accomplished with a single line: `System.IO.File.WriteAllText("selectedid.txt", SelectedId)`. This is slow and is a hack at IPC, but its more simple than other IPC methods.
Tim Coker