-1

My question is not answered by the question When do we need to set UseShellExecute to True? If you think it is, please explain how?

I need to set the useshellexecute to true for my windows form application project before the application process start, but I can not find where in the project I have to make the change?

It's a windows form application project in visual studio 2015.

The main entry point for the application looks like this:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
}

Where should I use ProcessStartInfo and specify UseShellExecute value?

EDIT: Big thank you to Matthew Watson who suggested I change the Environment CurrentDirectory which solved the issue! I will mark your anser as the correct one if you post it as an answer!

Community
  • 1
  • 1
user5825579
  • 105
  • 3
  • 15
  • "_…I need to set the useshellexecute to true for my windows form application…_" — That makes absolutely no sense to me. – Uwe Keim Apr 19 '16 at 11:40
  • When launching my application from it's folder, the working directory path is the folder I launched it from. But when I execute it from a shortcut somewhere the working directory path changes to System32. I googled and found I need to change useshellexecute to true but can not find where / how to change it? – user5825579 Apr 19 '16 at 11:45
  • 1
    If you look at the properties for the shortcut for your application, it has a "Start in" settings. I'm guessing that is either empty or pointing to the wrong folder. It should specify the folder that contains your exe. – Matthew Watson Apr 19 '16 at 11:50
  • How about simply [changing your working directory](http://stackoverflow.com/questions/1590097/how-do-i-get-set-a-winforms-applications-working-directory)? – Uwe Keim Apr 19 '16 at 11:54
  • You could also try putting this at the start of your program: `Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;` – Matthew Watson Apr 19 '16 at 11:55
  • @Matthew Watson who suggested I change the Environment CurrentDirectory which solved the issue! I will mark your anser as the correct one if you post it as an answer! – user5825579 Apr 19 '16 at 12:02

2 Answers2

0

You could use ProcessStartInfo and specify UseShellExecute value.

ProcessStartInfo startInfo = new ProcessStartInfo("exepath");
startInfo.Arguments = args; //argument
startInfo.UseShellExecute = true;

Process.Start(startInfo);
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0

You could try putting this at the start of your program:

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

That will ensure that your program's current working directory (CWD) is always set to the same folder as the executable when your program starts up.

I suspect what's happening is that the shortcut you're using has a missing or incorrect folder for its "Start in" property. Fixing that would be another approach (right-click the shortcut, select Properties and then the Shortcut tab, and enter the correct folder in the Start in section.)

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276