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

kento

KENTO⚽️XR Engineer😎

Posted on October 29, 2022

【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
💖 💪 🙅 🚩
kento
KENTO⚽️XR Engineer😎

Posted on October 29, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related