2013-03-30 57 views
3

我在VS2010中有一個解決方案。在該解決方案下,我有我的主要WPF應用程序,包括所有用戶界面,一對庫以及我想在WPF應用程序中單擊按鈕時運行的控制檯應用程序。我的解決方案結構與此類似:從不同的項目運行控制檯應用程序

- Solution 
    - WPF App [this is my startup project] 
    - Library 
    - Another library 
    - Console application 

現在我已經做了一些狩獵周圍,我發現人們在尋找如何引用代碼和類,也解決這之中,我找到路徑的可執行文件,並將其作爲新進程運行。但是,這需要知道絕對路徑,甚至是相對路徑,我想知道是否這是我可以啓動應用程序的唯一方式,即使它處於相同的解決方案中?

回答

5

是的,那是真的。您必須知道可執行文件的路徑,無論是絕對路徑還是相對路徑。但這不是故障。你爲什麼不把你的WPF exe和Console exe文件放在同一目錄下,或者像bin\myconsole.exe這樣的子目錄中?當創建新的Process時,只需將Console exe的名稱傳遞給Process.Start(),Windows就會找到您的可執行文件。

using System; 
using System.Diagnostics; 
using System.ComponentModel; 

namespace MyProcessSample 
{ 
class MyProcess 
{ 
    // Opens the Internet Explorer application. 
    void OpenApplication(string myFavoritesPath) 
    { 
     // Start Internet Explorer. Defaults to the home page. 
     Process.Start("IExplore.exe"); 

     // Display the contents of the favorites folder in the browser. 
     Process.Start(myFavoritesPath); 
    } 

    // Opens urls and .html documents using Internet Explorer. 
    void OpenWithArguments() 
    { 
     // url's are not considered documents. They can only be opened 
     // by passing them as arguments. 
     Process.Start("IExplore.exe", "www.northwindtraders.com"); 

     // Start a Web page using a browser associated with .html and .asp files. 
     Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm"); 
     Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp"); 
    } 

    // Uses the ProcessStartInfo class to start new processes, 
    // both in a minimized mode. 
    void OpenWithStartInfo() 
    { 
     ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe"); 
     startInfo.WindowStyle = ProcessWindowStyle.Minimized; 

     Process.Start(startInfo); 

     startInfo.Arguments = "www.northwindtraders.com"; 

     Process.Start(startInfo); 
    } 

    static void Main() 
    { 
     // Get the path that stores favorite links. 
     string myFavoritesPath = 
      Environment.GetFolderPath(Environment.SpecialFolder.Favorites); 

     MyProcess myProcess = new MyProcess(); 

     myProcess.OpenApplication(myFavoritesPath); 
     myProcess.OpenWithArguments(); 
     myProcess.OpenWithStartInfo(); 
    } 
} 
} 

Look here

+0

乾杯,我想我會跟着那個。 :) –

相關問題