DEV Community

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

Posted on

3

【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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs