4

I have a solution in VS2010. Under the solution, I have my main WPF application, with all the user interface, a couple of libraries, and a console application that I want to run when I click a button in my WPF application. My solution structure is similar to this:

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

Now I have done some hunting around, and I've found people looking for how to reference code and classes, and also a solution to this being that I find the path of the executable, and run it as a new process. However, this requires knowing an absolute path, or even a relative path, and I was wondering if that's the only way I can start an application, even though it's in the same solution?

Connor Deckers
  • 2,447
  • 4
  • 26
  • 45

1 Answers1

6

Yes,that is true. You must know the path to the executable, either absolute or relative. But that is no breakdown. Why don't you just put your WPF exe and Console exe in the same directory or in a subdirectory like in bin\myconsole.exe? When creating a new Process, just pass the name of the Console exe to Process.Start() and Windows will find your executable.

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.

bash.d
  • 13,029
  • 3
  • 29
  • 42