Link to home
Start Free TrialLog in
Avatar of smoyano
smoyano

asked on

Passing variable to a launch application

Hello,

We've developed a windows application that is installed locally with MS click-once installation.  I would like create another windows application that would launch the first applicaiton and I was able to do this by calling the application reference in the start menu.  But, I am unable to pass a variable to the application referecne in the start menu.  On the development machine if I call the Program1.exe the variable is passed.  But since this application would be installed on a client machine I am calling the application reference in the start menu.  How can I pass the variable to the program1 if I am launching it from its application reference?

Thanks,
Kim
Process objProcess = new Process();
objProcess.StartInfo.FileName = "C:\\Documents and Settings\\user\\Start Menu\\Programs\\Program1\\Program1.appref-ms";
objProcess.StartInfo.Arguments = "hello";
objProcess.Start();

Open in new window

Avatar of WhiteMeat
WhiteMeat
Flag of United States of America image

If this variable gets set at the time of the app install, couldn't you save this info in an .ini file and pick it up when the user launches the app from the Start menu?
How I did it...
 

ProcessStartInfo process = new ProcessStartInfo( );
process.FileName         = @"Watchlist.exe";
process.WorkingDirectory = @"C:\Program Files\XXX\Watchlist"; 
process.Arguments        = GetArguments( );
 
try
{
   Process.Start( process );
}
catch( InvalidOperationException ex )
{
   ErrorMessage.Instance.Show( ex.Message, Resources.WatchlistNotFound, Resources.Error );
   return;
}
 
static string GetArguments( )
{
   StringBuilder arguments = new StringBuilder( );
   arguments.AppendFormat( @"-language ""{0}""", Application.CurrentCulture );
 
   return arguments.ToString( );
}

Open in new window

Avatar of smoyano
smoyano

ASKER

Thanks Psdavis.  I've been using your example as shown.  How can the receiving program (Program1) pickup the argument sent by the calling program?  I've tried the following in the receiving program in the Program.cs, but it is not picking up the arguments sent.

Thanks,
Kim
//Calling program
private void btnCallProgram_Click(object sender, EventArgs e)
{
    string path = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
            
    Process objProcess = new Process();
    ProcessStartInfo process = new ProcessStartInfo();
    process.FileName = @"Program1.appref-ms";
    process.WorkingDirectory = @"C:\Documents and Settings\testdir\Start Menu\Programs\Program1";
    process.Arguments = GetArguments();
 
    try
    {
         Process.Start(process);
    }
    catch (InvalidOperationException ex)
    {
         MessageBox.Show("Error: ", ex.Message);
         return;
    }
}
 
static string GetArguments()
{
      StringBuilder arguments = new StringBuilder();
      arguments.AppendFormat(@"-language ""{0}""", Application.CurrentCulture);
      arguments.Append("var1");
 
      return arguments.ToString();
}
 
// Receiving program Program.cs
 
static void Main(String[] args)
{
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     if (args.Length>0)
            Application.Run(new Form1(args[0].ToString()));
     else
            Application.Run(new Form1());
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of psdavis
psdavis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smoyano

ASKER

Thanks psdavis.  The problem appears to be that I am calling an application link that then calls the Program1.exe.  The application link does not appear to pass the variable along.  Do you know how I can programically find what the directory path is that the application link is pointing to?  I thien could call the Program1.exe directly.  

Thanks you,
Kim