Boardy
Boardy

Reputation: 36205

Running `mklink` in ProcessStartInfo

I am working on a c# program that needs to be compatible with Windows and Linux (Mono).

I am trying to create a symbolic link in both platforms and I am using ProcessStartInfo in order for this to work. I haven't tried this on Linux yet but on Windows I am using the following code

ProcessStartInfo process = new ProcessStartInfo();

                    process.CreateNoWindow = true;
                    process.UseShellExecute = false;
                    process.FileName = "mklink";
                    process.WindowStyle = ProcessWindowStyle.Hidden;
                    process.Arguments = "/D " + webFolder + "MyFolder" + webFolder + "MyFolder_" + version;
                    Process.Start(process);

When I run the above code I get

System.ComponentModel.Win32Exception: The system cannot find the file specified

If I run mklink in command prompt it works fine.

I've had a look on Google and it says about doing a [DllImport("kernel32.dll")] but this isn't going to work on Linux.

Thanks for any help you can provide.

Upvotes: 9

Views: 4453

Answers (1)

Eric J.
Eric J.

Reputation: 150108

mklink is a command of the cmd.exe program, not a stand-alone program.

To run mklink, you have to actually invoke cmd.exe with an appropriate set of parameters, like this:

ProcessInfo = new ProcessStartInfo("cmd.exe", "/c mklink " + argumentsForMklink);

Upvotes: 24

Related Questions