简体   繁体   English

使文件路径动态化

[英]Making a file path dynamic

I just want to Code an Launcher for the Game i currently making, but i ran into an Problem and i am an absolutely beginner as a Programmer. 我只想为当前正在制作的游戏编写启动器,但遇到一个问题,并且作为程序员,我绝对是一个初学者。

Currently i have a Start Button with looks like this 目前我有一个“开始”按钮,看起来像这样

private void startbtn_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("C:\\Users\\Windows\\Desktop\\Folder\\Underfolder\\Game.exe");
}

But i try to make it Dynamic, so the Launcher can work which ever the User install the Data (There is an Installer for everything) also the launcher is located in the same directory as the Game.exe 但是我尝试使其动态化,因此无论用户安装数据是什么,启动器都可以工作(所有内容都有安装程序),而且启动器与Game.exe位于同一目录中

(The Code looks weird on the Post but it is correct) (该代码在帖子上看起来很奇怪,但它是正确的)

The most reliable way I've found to do this is (assuming Game.exe is located in the same path as your Launcher.exe as you mentioned in your post): 我找到的最可靠的方法是(假设Game.exe与您在帖子中提到的Launcher.exe位于同一路径):

var launcherExeDirectory = AppDomain.CurrentDomain.BaseDirectory;
var gameExeFullPath = Path.Combine(launcherExeDirectory, "Game.exe");

Then you can just do something like: 然后,您可以执行以下操作:

Process.Start(gameExeFullPath);

Just use GetCurrentDirectory 只需使用GetCurrentDirectory

Process.Start(System.IO.Directory.GetCurrentDirectory + @"\game.exe");

///Or

Process.Start(AppDomain.CurrentDomain.BaseDirectory + @"\game.exe");

///or

Process.Start(Application.ResourceAssembly.Location + @"\game.exe");

///or

Process.Start( System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\game.exe");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM