DEV Community

KENTO⚽️XR Engineer😎
KENTO⚽️XR Engineer😎

Posted on

【Unity】How to launch another exe placed at a relative path of the running exe.

Intro

I faced the situation that I would like to launch another exe placed at a relative path of the running exe.

So I write the way down on this article so I won't forget.

Sample Code

using System;
using System.Diagnostics;
using System.IO;
using UnityEngine;


public static class Launcher
{
    /// <summary>
    /// relative pass
    /// </summary>
    private static string path = "..\\..\\..\\target.exe";

    /// <summary>
    /// launch target exe and kill self
    /// </summary>
    public static void LaunchAndKillSelf()
    {
        //move current directory
        Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

        //start process
        Process process = new Process();
        process.StartInfo.FileName = path;
        process.Start();

        //kill self
        Application.Quit();
    }
}

Enter fullscreen mode Exit fullscreen mode

Working directory is changed if the running exe was launched by another exe.

The code below is a workaround for that.

Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)