Kurisuchin
Kurisuchin

Reputation: 155

Executing another Application with selected file using a button (C# Programming)

To start, I'm a newbie with C# Programming and I've tried googling for solutions about my problem but it seems that i cannot find one, or simply too unlucky or too blind to spot one. I'm using Microsoft Visual Studio 2005.

Anyways. I was assigned to modify/create an automated test environment input application. The said application already has a function to run/start a CANoe program with a pre-defined file or if it's already running, stop the program.

private void button1_Click(object sender, EventArgs e)
    {
        // Execute CANoe(Obtain CANoe application objectg)
        mApp       = new CANoe.Application();
        mMsr       = (CANoe.Measurement)mApp.Measurement;

        try
        {
            mApp.Open("C:\\Users\\uidr3024\\Downloads\\SRLCam4T0_Validation_ControlTool\\cfg\\SVT_SRLCam4T0_025B.cfg", true, true);
        }
        catch (System.Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }
    }


    private void button2_Click(object sender, EventArgs e)
    {
        // Finish CANoe
        if (mApp != null) {
            mApp.Quit();            
        }
        // Release the object
        fnReleaseComObject(mMsr);
        fnReleaseComObject(mApp);
    }

What I wanted to do now is to have an OpenFileDialog box that will show a selection of files and user will be able to browse and select any file to start the CANoe program with the selected file and not just the file path that's been input in the code along the "mApp.Open()" syntax. I've tried this:

private void button5_Click_1(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.InitialDirectory = @"C:\Users\uidr3024\Downloads\SRLCam4T0_Validation_ControlTool\cfg";
        openFileDialog1.Title = "Browse Configuration Files";
        openFileDialog1.CheckFileExists =  true;
        openFileDialog1.CheckPathExists = true;
        openFileDialog1.Filter = "CANalyzer/CANoe Configuration (*.cfg)|*.cfg |All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 1;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = openFileDialog1.FileName;
        }
    }

I've tried this code that I often see in the Web and in the Tutorials but I don't know how to incorporate it with the button for running the CANoe program so that when user clicks the Open button from the Dialog Box, the file path would be shown in the textbox(optional) and/or when the user then clicks the Start CANoe, CANoe program would start with the selected .cfg file.

Am I making any sense here? Or am I doing the right thing here?

Btw, I found these... And I'm using a CANoe library for these, ofc.

    #region "***** CANoe Object definition *****"
    private CANoe.Application      mApp          = null; // CANoe Application CANoeƒAƒvƒŠƒP[ƒVƒ‡ƒ“
    private CANoe.Measurement      mMsr          = null; // CANoe Mesurement function CANoe‘ª’è‹@”\
    private CANoe.Variable         mSysVar       = null; // System variable ƒVƒXƒeƒ€•Ï”
    private CANoe.Variable         mSysVar_start = null; // System variable ƒVƒXƒeƒ€•Ï”
    #endregion

Upvotes: 0

Views: 740

Answers (1)

Gary Wright
Gary Wright

Reputation: 2469

I think you have done most of the hard work, unless I've missed something I think you just need something like this in your button1_Click method:

if( textBox1.Text != String.Empty && System.IO.File.Exists(textBox1.Text) )
{
    // The textbox has a filename in it, use it
    mApp.Open(textBox1.Text, true, true);
}
else
{
    // The user hasn't selected a config file, launch with default
    mApp.Open("C:\\Users\\uidr3024\\Downloads\\SRLCam4T0_Validation_ControlTool\\cfg\\SVT_SRLCam4T0_025B.cfg", true, true);
}

Upvotes: 0

Related Questions