-1

I have this code -

private void button1_Click(object sender, EventArgs e) {
    System.Diagnostics.Process.Start(textbox1.Text);
}

The name of my project is NewFormX and when I typed NewFormX.exe in the Text Box and clicked the button it worked fine. But when I tried the same with Google Chrome which is chrome.exe it's showing that The system cannot find the file specified(I want to open the file at runtime).

I almost scrolled till the third page of my browser but still didn't find any explanation for what I was looking for.

And, when I tried the same with web links i.e. https://stackoverflow.com/ or https://www.google.com/ the error was still occurring.

Your help will be appreciated.

NoNAME
  • 65
  • 8
  • If you want to start an application, you need to know its path. Provide the full path in the textbox. And you can't use `Process.Start()` to open links (at least not directly). – PMF Apr 10 '21 at 20:08
  • If you want the default WebBrowser to open up and navigate to a HTTP address using `Process.Start()`, you need `UseShellExecute = true`. If you're using .Net Core / .Net 5+, you need to set it explicitly, since the default is `false`. In .Net Framework the default is `true`. -- With `UseShellExecute = false`, you need to provide the full path of the executable as `FileName` and the Link as `Arguments`. – Jimi Apr 10 '21 at 20:40

2 Answers2

1

There is one way to do it, that I know of.

You could execute a cmd command to start chrome which will start it without a path to it. Most apps have a shortcut you can use via the "Run Dialog" or CMD.

This code will execute the start command for chrome, if it is installed. You can make it a try catch statement if it cant find chrome for example it will just throw an error for the user etc.

string cmdCommand = "/C start chrome";
System.Diagnostics.Process.Start("CMD.exe", cmdCommand);

This code can also work for other apps that can be run via the run dialog with one shortcut for example, explorer.exe or just explorer.

If you have any other questions or errors, please provide some debugging data for example: errors, your VS Version the framework you are on etc.

Edit: You can also run a link just by using Process.Start but it will start it in the default browser instead.

System.Diagnostics.Process.Start("https://google.com/");

If you want to run your own apps or other programs you would need the full path of the app.

Edit 2: There is one possibility to do this, it involves creating a try catch statement where it would search from the C:\ Directory for any files with the specific name eg. chrome.exe, this would take too long and it would be a pain to also implement the searching, if possible of other drives, Ignoring CD/DVD drives etc.

I'm not sure what you are making but I will try to find a solution to this with what I explained.

//code for the solution, coming soon

Edit 3: after a long time of searching and also kind of forgetting this, It really doesn't make sense to make such a solution as it is too complex and requires too much for a simple 'app opener'.

Tested on a default C# Windows Form on the .NET Framework 4.7.2 in Visual Studio 2019 on Windows 10 Pro 64-Bit

SimpleCoder
  • 474
  • 4
  • 18
1

You can do this -

using System.Diagnostics;
[...]

private void button1_Click(object sender, EventArgs e) {
    Process p = new();
    p.StartInfo.UseShellExecute = true;
    p.StartInfo.FileName = "Your file name with extension/File path";

    p.Start();
}
Naitik
  • 55
  • 1
  • 8