none
Modify application 'start in' and 'target' the same way a shortcut does RRS feed

  • Question

  • When you create an application shortcut within windows explorer, you have the option to modify the properties of the shortcut by right click> properties.

    There are two properties that I need to modify for my application to run correctly, 'Target:' and 'Start in'.

    I am modifying the 'Target' property to start a different program which launches my program using an argument so looks something like...:

    C:\External_Program.exe -start"C:\My_Application.exe"

    My_Application is the C# application that I am developing in visual studio. External_Program is another off the shelf program I have purchased.

    I am modifying the 'Start in' property of the shortcut to point to the folder that External_Program is located in. Such as:

    C:\

    ....

    So I need to find a way, within Visual Studio to run the application I'm developing ('My_Application') and run it with the same properties as I would if I were to create a shortcut and modify the 'Start in' and 'Target' properties of the shorcut. At the moment I have to build the project, go to the debug/release folder, create a shortcut of the 'My_Application.exe' and then manually modify the shortcut.

    This is then further complicated because I want to combine this project into a solution with other projects, reference 'My_Application' project and make calls to it from other projects, such that I can't just program the creation of a shortcut and then launch it as a process.

    I have a solution with two projects within it. I am using project_1 to make calls to project_2 ('My_Application') within the solution. It is project_2 ('My_Application') I need to assign these properties to.

    Please advise if you know is this is possible?

    I have tried modifying the registry Path values but haven't figured out how to modify the 'Start in' and 'Target' values within the registry.

    Best regards

    Saturday, March 28, 2020 10:25 AM

Answers

  • I think you're missing my question, I don't know how I can make it more clear


    Your question makes no sense in the context of instantiating a form in an already running process. The external launcher is not relevant.  Your notion of the "Target" is not relevant.

    So I say for the second time that what you want to do is not possible.

    • Marked as answer by AJF_1 Monday, March 30, 2020 1:27 PM
    Monday, March 30, 2020 1:26 PM

All replies

  • You can create shortcuts with the parameters you want with WSH

    For example, a shortcut to Notepad with various parameters =>

    // Add reference to : Windows Script Host Object Model              
    WshShell wshShell = new WshShell();
    string sShortcut = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Notepad.lnk";
    IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(sShortcut);
    shortcut.Description = "Notepad Shortcut";
    shortcut.Hotkey = "Ctrl+Shift+N";
    shortcut.IconLocation = @"%SystemRoot%\System32\shell32.dll,2";
    shortcut.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
    shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
    shortcut.Save();

    Saturday, March 28, 2020 10:57 AM
  • When you create an application shortcut within windows explorer, you have the option to modify the properties of the shortcut by right click> properties.

    There are two properties that I need to modify for my application to run correctly, 'Target:' and 'Start in'.

    I am modifying the 'Target' property to start a different program which launches my program using an argument so looks something like...:

    C:\External_Program.exe -start"C:\My_Application.exe"

    My_Application is the C# application that I am developing in visual studio. External_Program is another off the shelf program I have purchased.

    I am modifying the 'Start in' property of the shortcut to point to the folder that External_Program is located in. Such as:

    C:\

    ....

    So I need to find a way, within Visual Studio to run the application I'm developing ('My_Application') and run it with the same properties as I would if I were to create a shortcut and modify the 'Start in' and 'Target' properties of the shorcut. At the moment I have to build the project, go to the debug/release folder, create a shortcut of the 'My_Application.exe' and then manually modify the shortcut.

    I wrote a small launcher program and used it in a C# winform project to launch the C# application.  The example application just displays its working directory when it starts.

    C# Project properties were set to the following -

    When running this in Visual Studio, the debugger starts the launcher program.  The launcher program starts the C# Winform application and then terminates so the debugging session ends.

    C# application startup -

    If you want to debug the Winform application you would need to attach the debugger to it since the initial debugging session ends when the launcher program terminates.

    This is then further complicated because I want to combine this project into a solution with other projects, reference 'My_Application' project and make calls to it from other projects, such that I can't just program the creation of a shortcut and then launch it as a process.

    I have a solution with two projects within it. I am using project_1 to make calls to project_2 ('My_Application') within the solution. It is project_2 ('My_Application') I need to assign these properties to.

    Its not clear to me what you want to accomplish here.  Projects don't call projects.  Explain your objectives in terms of applications and class libraries where applicable.

    Saturday, March 28, 2020 12:46 PM
  • Hi, thanks for your response.

    Within my solution I have two projects. Project_A and Project_B. 

    Project_A references Project_B ie...

    namespace Project_A { public partial class Form1 : Form { ProjectB.MainForm pBForm = null;

            public Form1()
            {

    string filepath = "*"; //user selects a file through OpenFileDialog code omitted

    pBForm = new ProjectB.MainForm(filepath );


    I haven't included the full code but when project_A form loads, it firsts opens an OpenFileDialog window for the user to select a file. the file path (filepath) is then passed into pBForm to initialise it.

    It is pBForm that I need to start in a specific folder with start commands, very similar to how you appeared to be working through the solution above.

    Best regards




    • Edited by AJF_1 Saturday, March 28, 2020 1:17 PM
    Saturday, March 28, 2020 1:16 PM
  • Why can't you simply use Process.Start to start the Project_B application along with any parameters that you want to pass from the Project_A application?
    Saturday, March 28, 2020 1:21 PM
  • Passing variables back and forth between the two projects is possible when treating it as a form, also handling the form, docking, scaling etc is all very straightforward when loading Project_B as a new form() as opposed to an external application
    Saturday, March 28, 2020 1:39 PM
  • What exactly do you mean when you say "It is pBForm that I need to start in a specific folder"?

    Do you want to pass a string parameter to the form?  The working directory is for an application.

    Saturday, March 28, 2020 2:08 PM
  • Is this what you mean?

    namespace CSFormTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = Environment.CurrentDirectory;
                ProjectB.PBForm pbform = new ProjectB.PBForm(textBox1.Text);
                pbform.Show();
            }
        }
    }

    and

    namespace ProjectB
    {
        public partial class PBForm : Form
        {
            public PBForm()
            {
                InitializeComponent();
            }
    
            public PBForm(string parameter)
            {
                InitializeComponent();
                textBox1.Text = parameter;
            }
        }
    }


    • Edited by RLWA32 Saturday, March 28, 2020 2:16 PM
    Saturday, March 28, 2020 2:14 PM
  • So I've been trying with your example here:

    Which appears to do just what I need - but how do I build a project that does this all the time - not only when it is built in debug mode?

    Project_A is launching Project_B. Project_B has the above settings applied but Project_A does not.

    How can I create an instance of Project_B with the settings above when the startup project is Project_A because it doesn't seem to launch Project_B with those settings when I launch Project_B from Project_A. ie

    Project_B.MainForm projectB = new Project_B.MainForm()

    It only seems to launch it with the start arguments, defined in picture above, when you launch Project_B as the startup project. 

    Please advise how I can set it such that Project_B runs with the settings seen in the project debug settings picture above, whenever the Project_B.exe is launched regardless of in debug mode or not

    Best regards

    Saturday, March 28, 2020 3:10 PM
  • The project properties (Debug/Release) relate to starting applications.

    You seem to think that starting applications is the same thing as referencing and instantiating a form.  They are not the same.

    Project_B.MainForm projectB = Project_B.MainForm() does NOT start an application.  Project properties for starting applications are not relevant to this circumstance.

    The working directory for the application is the same for both forms unless you change it in code when switching between forms.

    Saturday, March 28, 2020 3:25 PM
  • When I load my project, Project_A, I'm making a reference to Project_B.

    Project_B needs to be loaded with 'target' and 'Start in' as defined in the following picture

    Please advise how I can do this from Project_A

    Best regards

    Saturday, March 28, 2020 3:40 PM
  • When I load my project, Project_A, I'm making a reference to Project_B.

    Project_B needs to be loaded with 'target' and 'Start in' as defined in the following picture

    Please advise how I can do this from Project_A

    Best regards

    Again, what do you mean by "Project_B needs to be loaded with 'target' and 'Start in...' "?

    If you are talking about starting an application, then use Process.Start and Startinfo.

    If you are talking about instantiating a form, then pass a parameter to the form in the constructor and set the working directory through code.

    Saturday, March 28, 2020 3:56 PM
  • Yes I'm talking about instantiating a form, called Project_B.

    Is it possible to instantiate Project_B form through external launcher with commands line arguments and change the working directory?

    Best regards

    Saturday, March 28, 2020 4:15 PM
  • Yes I'm talking about instantiating a form, called Project_B.

    Is it possible to instantiate Project_B form through external launcher with commands line arguments and change the working directory

    No.  The external launcher is a program that starts applications.  The "Start in" folder that is set for the external launcher will be the current directory for the application that it starts.  That directory will be the current directory for all the forms instantiated by the application, including the Project_B form,  unless you change the current directory in code.
    Saturday, March 28, 2020 4:37 PM
  • That's the bit I'm struggling with - changing the directory seems OK but trying to change Project_B to run as an argument from an external application when instantiating Project_B from Project_A, I have no idea - if anybody knows how to do this, please advise

    Best regards

    Saturday, March 28, 2020 9:56 PM
  • That's the bit I'm struggling with - changing the directory seems OK but trying to change Project_B to run as an argument from an external application when instantiating Project_B from Project_A,

    I have no idea what this means.
    Saturday, March 28, 2020 10:32 PM
  • Hi AJF_1,

    Thank you for posting here.

    After reading your conversation over and over, I think the "Process" mentioned by RLWA32 may be the solution for you.

    Take a look at the code below. When you start this program, it will immediately call OpenFileDialog to select a file, then use Process to start the second program and pass the corresponding parameters in, which seems to be in line with your logic.

             private void Form1_Load(object sender, EventArgs e)
            {
                string filePath = "";
                using (OpenFileDialog fileDialog = new OpenFileDialog())
                {
                    if (fileDialog.ShowDialog() == DialogResult.OK)
                    {
                        filePath = fileDialog.FileName;
                    }
                }
    
                Process process = new Process();
                process.StartInfo.FileName = "C:\\HelloWorld.exe"; 
                process.StartInfo.WorkingDirectory = "";           
                process.StartInfo.Arguments = "";
                process.Start();
    
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, March 30, 2020 6:01 AM
  • Thank you Timon and RLWA32

    The proposed solutions work to an extent but limits my much needed control over the process started using proces.start. I'll try to rephrase my question.

    I have a solution with two projects, project_A and project_B. An exe is generated for both projects when I build the solution. 

    Project_A is my StartUp project. Within Project_A I.... > Right click references>add reference>projects>Project_B

    At the top of Project_A i have added a line 'using Project_B'

    Within the code of Porject_A, I want to load an instance of Project_B, so that all the methods and variables are available to me, such as:

    Project_B.MainForm projB = new Project_B.MainForm()

    The problem is that when you load Project_B this way, it will not work because for the Project_B to function correctly it needs to be loaded via another program with arguments, which I would normally define within either one of three methods:

    1 - Within Visual Studio as demonstrated by RLWA32:

    or

    2 - Within the shortcut settings

    3 - starting it as a process as demonstrated by Timon:

                Process process = new Process();
                process.StartInfo.FileName = "C:\\HelloWorld.exe"; 
                process.StartInfo.WorkingDirectory = "";           
                process.StartInfo.Arguments = "";
                process.Start();

    But these options only work when loading Project_B in isolation.

    I need to keep control of Project_B by loading it within Project_A using the:

    Project_B.MainForm projB = new Project_B.MainForm() method

    so that I can continue to use projB with access to all the methods and variables etc within Project_B

    So what I need to do would be something like

    Project_B.MainForm projB = null;

    projB.startinfo.arguments = "";

    projB.startinfo.workingdirectory = "";

    projeB = new Project_B.MainForm();

    But this doesn't appear possible in this way - do you know of any solution to this problem?

    Best regards

    Monday, March 30, 2020 7:19 AM
  • Hi Ajf_1,

    You can add a reference in Project_A, select the "Project" tab, then select Project_B, then use using to add a namespace in Project_B, and then you can call the Project_B method.

    using System;
    using ConsoleApp2;
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Program1 is a class in ConsoleApp2
                Program1 program1 = new Program1();
                program1.Dowork();
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
    }

    In fact, if you just want to call the methods and properties of Project_B without displaying any Project_B interface, it is better to set Project_B as a class library project.

    Update: In this way, Program1 in ConsoleApp2 need be public.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, March 30, 2020 7:57 AM
  • Hi Timon

    Your response shows the first part to what I need to do however, Project_B is a form, not a class. But now I need to code it such that Project_B starts as an argument within an external application, within a defined working directory. Such as shown in the following settings:

    But these settings don't apply when you're starting Project_B as a form from Project_A so this is what I need a solution for

    Best regards


    • Edited by AJF_1 Monday, March 30, 2020 8:08 AM
    Monday, March 30, 2020 8:07 AM
  • Hi AJF_1,

    Form is a class, I still use the above method.

    But there are some differences from calling ConsoleApp.

    [STAThread] static void Main(string[] args) { Form1 form1 = new Form1(); form1.MethodInForm();

    Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }


    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 30, 2020 8:18 AM
  • If you need to run with certain parameters, you can add parameter construction in form1, and then call this constructor to pass parameters.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 30, 2020 8:19 AM
  • Hi Timon

    Can you please advise how to do this?

    It feels like I need to do something like this:

    Project_B.MainForm projB = null;

    projB.startinfo.arguments = "";

    projB.startinfo.workingdirectory = "";

    projeB = new Project_B.MainForm();

    But this isn't an option - have you got an example for what you describe as constructor parameters?

    Best regards

    Monday, March 30, 2020 8:23 AM
  • Hi AJF_1,

    Something like this:

    Code in Form:

           private string text;
            public Form1(string para1)
            {
                InitializeComponent();
                text = para1;
            }
            public void MethodInForm()
            {
                MessageBox.Show(text);
            }

    Code in consoleApp:

            [STAThread]
            static void Main(string[] args)
            {
                Form1 form1 = new Form1("parameter1");
                form1.MethodInForm();
    
    
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }


    Result:

    But I suddenly thought of a problem, you seem to say another off the shelf program you have purchased.

    Does this mean we can't modify its code or open it in Visual Studio?

    If so, your question seems to be how to call part of this purchased program's functionality in your own program.

    This seems impossible.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 30, 2020 8:39 AM
  • Hi Timon

    The off-the-shelf application is the launcher. So if we go back to the shortcut example.

    I make the project (project_B) within visual studio that generates an exe.

    I then manually create a shortcut of that exe then add the following settings within the shortcut:

    target = C:\OffTheShelfAppDir\OffTheShelfApp.exe -start "C\:Project_B"

    start in = C:\OffTheShelfAppDir

    I can do the same thing using the following settings when launching Project_B:

    But then when I try and run this from Project_A within the solution that contains Project_A and Project_B, I've got no known way at the moment for running Project_B from Project_A (as a new instance ie Project_B.MainForm projB = new Project_B.MainForm();) using the OffTheShelf launcher app, setting Project_B as an argument for the launcher app.

    Please advise if you know if this is possible?

    Best regards

    Monday, March 30, 2020 8:49 AM
  • I am a bit confused.

    Let me organize my thoughts.

    According to my understanding, you want to call OffTheShelf launcher app from project_A, and then launch project_B when you launch OffTheShelf launcher app.

    Does this your mean?

    What role the OffTheShelf launcher app played in this process.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 30, 2020 9:09 AM
  • The OP's questioning makes no distinction between an application (.exe) and an instantiated form.

    I have already demonstrated using a form class constructor that takes a parameter and have tried to get the OP to realize that in the context of the question that Project_B is NOT an application but is an instantiated form.

    My best guess at what the OP wants to do is to instantiate a From class (presumably in some running process) from the external launcher program in the same manner as Process.Start starts an executable. 

    I have tried to explain that this is not possible,


    • Edited by RLWA32 Monday, March 30, 2020 9:35 AM
    Monday, March 30, 2020 9:34 AM
  • I think the following is what you need to do -

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Diagnostics;
    
    
    namespace CSFormTest
    {
        public partial class Form1 : Form
        {
            string strcurrentdir;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                strcurrentdir = Environment.CurrentDirectory;
                ProjectB.PBForm pbform = new ProjectB.PBForm(@"F:\RLWA32");  //Pass different directory that PBForm will set as currentdirectory
                pbform.Show();
            }
    
            private void Form1_Activated(object sender, EventArgs e)
            {
                Debug.Print("Form1_Activated current directory is {0}", Environment.CurrentDirectory);
                System.IO.Directory.SetCurrentDirectory(strcurrentdir);
                Debug.Print("Form1 current directory is now set to {0}", Environment.CurrentDirectory);
                textBox1.Text = Environment.CurrentDirectory;
            }
        }
    }
    

    and ProjectB

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Diagnostics;
    
    namespace ProjectB
    {
        public partial class PBForm : Form
        {
            string strpbformdir;
    
            public PBForm()
            {
                InitializeComponent();
            }
    
            public PBForm(string parameter)
            {
                InitializeComponent();
                strpbformdir = parameter;
                Debug.Print("PBForm constructor - current directory is {0}", Environment.CurrentDirectory);
                System.IO.Directory.SetCurrentDirectory(parameter);
                Debug.Print("PBForm constructor - current directory is now set to {0}", Environment.CurrentDirectory);
            }
    
            private void PBForm_Load(object sender, EventArgs e)
            {
                textBox2.Text = Environment.CurrentDirectory;
            }
    
            private void PBForm_Activated(object sender, EventArgs e)
            {
                Debug.Print("PBForm_Activated current directory is {0}", Environment.CurrentDirectory);
                System.IO.Directory.SetCurrentDirectory(strpbformdir);
                Debug.Print("PBForm current directory now set to {0}", Environment.CurrentDirectory);
                textBox2.Text = Environment.CurrentDirectory;
            }
        }
    }
    

    Monday, March 30, 2020 10:23 AM
  • Hi

    This is OK for one of the parameters I'm trying to define- 'Start in' but doesn't account for the external off-the-shelf launcher that I want to run Project_B as an argument within.

    C:\External_Launcher.exe - start"C:\....Project_B.exe"

    To clarify - I need to define for Project_B, when starting from Project_B the 'start in' and 'target' the same way you can within a shortcut

    

    Best regards

    Andy

    Monday, March 30, 2020 12:39 PM
  • Hi

    This is OK for one of the parameters I'm trying to define- 'Start in' but doesn't account for the external off-the-shelf launcher that I want to run Project_B as an argument within.

    C:\External_Launcher.exe - start"C:\....Project_B.exe"

    To clarify - I need to define for Project_B, when starting from Project_B the 'start in' and 'target' the same way you can within a shortcut

    

    Best regards

    Andy

    You are utterly confused.  Decide whether you want to start a process or instantiate a form in an already running application.

    The external launcher is only relevant with respect to starting a process.

    • Edited by RLWA32 Monday, March 30, 2020 1:08 PM
    Monday, March 30, 2020 12:59 PM
  • I think you're missing my question, I don't know how I can make it more clear

    I want to instantiate a form - with 'start in' and 'target' the same way you would with a shortcut.

    You've given me half the solution above - with the following code:

    public PBForm(string parameter) { InitializeComponent(); strpbformdir = parameter; Debug.Print("PBForm constructor - current directory is {0}", Environment.CurrentDirectory); System.IO.Directory.SetCurrentDirectory(parameter); Debug.Print("PBForm constructor - current directory is now set to {0}", Environment.CurrentDirectory); } private void PBForm_Load(object sender, EventArgs e) { textBox2.Text = Environment.CurrentDirectory; } private void PBForm_Activated(object sender, EventArgs e) { Debug.Print("PBForm_Activated current directory is {0}", Environment.CurrentDirectory); System.IO.Directory.SetCurrentDirectory(strpbformdir); Debug.Print("PBForm current directory now set to {0}", Environment.CurrentDirectory); textBox2.Text = Environment.CurrentDirectory; }

    This is the solution for the current directory which addresses my problem for the 'start in'

    aspect, but does not answer my question about the 'target' section

    Monday, March 30, 2020 1:23 PM
  • I think you're missing my question, I don't know how I can make it more clear


    Your question makes no sense in the context of instantiating a form in an already running process. The external launcher is not relevant.  Your notion of the "Target" is not relevant.

    So I say for the second time that what you want to do is not possible.

    • Marked as answer by AJF_1 Monday, March 30, 2020 1:27 PM
    Monday, March 30, 2020 1:26 PM