从其他项目运行控制台应用程序

时间:2013-03-30 06:36:38

标签: c# projects

我在VS2010中有一个解决方案。在解决方案下,我有我的主要WPF应用程序,包含所有用户界面,几个库,以及我想在我的WPF应用程序中单击按钮时运行的控制台应用程序。我的解决方案结构类似于:

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

现在我已经做了一些狩猎,我发现人们正在寻找如何引用代码和类,以及解决这个问题的方法,我找到了可执行文件的路径,并将其作为一个新进程运行。然而,这需要知道绝对路径,甚至是相对路径,我想知道这是否是我启动应用程序的唯一方法,即使它在同一个解决方案中?

1 个答案:

答案 0 :(得分: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();
    }
}
}

here