Search code examples
c#infopath

Passing parameters from one app to the other


Following on from this thread Starting application before target application

I have an application which gets passed a parameter (a filename) and does some registry work before opening Microsoft InfoPath.

I need to open InfoPath with the parameter that was passed to the original application.

Here is how I open InfoPath

System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.Arguments = ConvertArrayToString(Constants.Arguments);
//prc.StartInfo.Arguments = "hello";
prc.StartInfo.FileName = Constants.PathToInfoPath;
prc.Start();

Note that when I set the Arguments to "hello" InfoPath pops up a message saying cannot find file "hello" however when I set it Constants.Arguments I get an error and Windows asks me if I want to debug or close the applicatiion.

Here is how I set Constants.Arguments in the Main(string[] args)

static void Main(string[] args)
{
    Constants.Arguments = args;
    //...
}

And here is ConvertArrayToString

private string ConvertArrayToString(string[] arr)
{

    string rtn = "";
    foreach (string s in arr)
    {
        rtn += s;
    }

    return rtn;

}

I suppose the format of the parameter is causing the error, any idea why?

The value of Arguments after being stringed is

c:\users\accountname\Desktop\HSE-000403.xml

Edit:

Thanks to N K's answer.

The issue is in order for my application to open when InfoPath files are opened, I have changed the name of INFOPATH.EXE to INFOPATH0.EXE and my application is called INFOPATH.EXE and is in the InfoPath folder, so when files are opened my application opens.

Now when I do not change the name (eg I leave it as INFOPATH.EXE) it works as expected, however if it is called anything other than that then I get the error.

Unfortunately I need my application to open first.


Solution

  • I tried the below and it's works fine. Let me know what you get with this. (Don't forget to change path to files)

    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Process prc = new System.Diagnostics.Process();
            prc.StartInfo.Arguments = string.Join("", Constants.Arguments);
            prc.StartInfo.FileName = Constants.PathToInfoPath;
            prc.Start();
        }
    }
    public class Constants
    {
        public static string PathToInfoPath = @"C:\Program Files (x86)\Microsoft Office\Office14\INFOPATH.EXE";
        public static string[] Arguments = new string[] { @"c:\users\accountname\Desktop\HSE-000403.xml" };
    }