2011-04-23 53 views
1

我相信我一直在採取正確的方法到目前爲止,但我希望有一個按鈕在我的電腦上啓動視頻遊戲。C#Windows應用程序 - 鏈接一個按鈕以啓動遊戲

到目前爲止,我有一個按鈕鏈接到一個過程:

void button2_Click(object sender, EventArgs e) 
{ 
    process1.Start(); 
} 

this.process1.EnableRaisingEvents = true; 
this.process1.StartInfo.Domain = ""; 
this.process1.StartInfo.FileName = "MBO\\marbleblast.exe"; 
this.process1.StartInfo.LoadUserProfile = false; 
this.process1.StartInfo.Password = null; 
this.process1.StartInfo.StandardErrorEncoding = null; 
this.process1.StartInfo.StandardOutputEncoding = null; 
this.process1.StartInfo.UserName = ""; 
this.process1.SynchronizingObject = this; 
this.process1.Exited += new System.EventHandler(this.Process1Exited); 

所以,在這裏,我曾經放置EXE(在一個我編碼),將推出子文件夾MBO下的「marbleblast.exe」相對於它的位置。

它似乎在工作,並試圖啓動遊戲,但是,它說它無法加載那裏的文件。我在沒有我的發射器的情況下測試了遊戲,並且它工作正常。我相信它試圖運行EXE,但不讓它使用其文件夾內的其他文件。

如果需要,我會給出更多細節。

如何讓遊戲正常運行?

回答

1

this.process1.StartInfo.WorkingDirectory = 「MBO \」;

在遊戲中存在sl programming的編程,它依賴於Environment.CurrentDirectory被設置正確。默認情況下,它與EXE所在的目錄相同。 upvoted的答案雖然重複了錯誤。爲了使這個聲明確實解決了這個問題,你現在依靠你的 CurrentDirectory被設置正確。如果它沒有設置在你認爲的地方,那麼它仍然無法工作。

程序當前目錄的問題是它可以被你不控制的軟件改變。經典示例是OpenFileDialog,其RestoreDirectory屬性設置爲默認值false。等等。

總是防守地編程並傳遞文件和目錄的完整路徑名。像c:\ mumble \ foo.ext一樣。爲了實現這一目標,請從Assembly.GetEntryAssembly()。位置開始,這是EXE的路徑。然後使用System.IO.Path類從中生成路徑名稱。正確的始終工作代碼是:

using System.IO; 
using System.Reflection; 
... 
     string myDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); 
     string gameDir = Path.Combine(myDir, "MBO"); 
     string gameExe = Path.Combine(gameDir, "marbleblast.exe"); 
     process1.StartInfo.FileName = gameExe; 
     process1.StartInfo.WorkingDirectory = gameDir; 
     process1.SynchronizingObject = this; 
     process1.EnableRaisingEvents = true; 
     process1.Exited += new EventHandler(Process1Exited); 
+0

仍然沒有工作,雖然我得到的基本概念。編輯:它調整了一些調整後!非常感謝! – 2011-04-23 16:10:50

2

嘗試添加此類似,設置Working Directory

this.process1.StartInfo.WorkingDirectory= "MBO\\"; 

什麼的。

+0

一旦我這樣做,它給了我一個錯誤,結果似乎比以前更糟糕。 – 2011-04-23 15:49:04

1

我是來自MBForums的Dobrakmato。你簡單的需要添加大理石爆炸的工作目錄。

this.process1.EnableRaisingEvents = true; 
this.process1.StartInfo.Domain = ""; 
this.process1.StartInfo.FileName = "MBO\\marbleblast.exe"; 
this.process1.StartInfo.WorkingDirectory = "pathto marbleblast.exe directory"; 
this.process1.StartInfo.LoadUserProfile = false; 
this.process1.StartInfo.Password = null; 
this.process1.StartInfo.StandardErrorEncoding = null; 
this.process1.StartInfo.StandardOutputEncoding = null; 
this.process1.StartInfo.UserName = ""; 
this.process1.SynchronizingObject = this; 
this.process1.Exited += new System.EventHandler(this.Process1Exited); 
+0

嘿Dobrakmato,不知道你在這裏!我遇到了一些問題。你的解決方案和上面的解決方案都不會產生結果。 – 2011-04-23 15:54:52

+0

我在開發MBMC時發現了這個解決方案。 WorkingDir也要求Torque Constructor和Gimp。 – 2011-04-23 16:00:04