Torbjörn Dahlgren
Torbjörn Dahlgren

Reputation: 111

Execute file from USB

Iam making a program that's a suppose run from a USB key. But i have problem running file that are in a folder.

Usb folder/file structure looks like:

I can execute the install.exe file but running this code

private void Icon_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("install.exe");
}

But how do i make it to start the file in the sub folder?

Upvotes: 1

Views: 256

Answers (2)

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29233

You can temporarily navigate to the directory, run the executable, then go back to where you were:

var dir = Environment.CurrentDirectory;
Environment.CurrentDirectory = Path.Combine(dir, "data", "install");
System.Diagnostics.Process.Start("install2.exe");
Environment.CurrentDirectory = dir;

Upvotes: 2

Bishoy
Bishoy

Reputation: 4025

My initial answer mistook your problem with wanting to autorun the file upon inserting the USB drive.

My understanding is that your question is how to use the code you provided to run a file in a sub folder. And here are a few options:

  1. Use a relative path ".\data install\install2.exe" and this will work if your program is started/executing from its own path.

  2. Read the current excutable path and use it to construct a new path to that sub folder (check this question for an example)

Upvotes: 0

Related Questions