C# 从其他项目运行控制台应用程序
我在VS2010中有一个解决方案。在该解决方案下,我有我的主WPF应用程序、所有的用户界面、几个库和一个控制台应用程序,当我单击WPF应用程序中的按钮时,我希望运行该应用程序。我的解决方案结构与此类似:C# 从其他项目运行控制台应用程序,c#,projects,C#,Projects,我在VS2010中有一个解决方案。在该解决方案下,我有我的主WPF应用程序、所有的用户界面、几个库和一个控制台应用程序,当我单击WPF应用程序中的按钮时,我希望运行该应用程序。我的解决方案结构与此类似: - Solution - WPF App [this is my startup project] - Library - Another library - Console application 现在我做了一些搜索,我发现人们在寻找如何引用代码和类,还有一个解决方案,我找到
- Solution
- WPF App [this is my startup project]
- Library
- Another library
- Console application
现在我做了一些搜索,我发现人们在寻找如何引用代码和类,还有一个解决方案,我找到了可执行文件的路径,并将其作为一个新进程运行。但是,这需要知道绝对路径,甚至是相对路径,我想知道这是否是启动应用程序的唯一方法,即使它在同一个解决方案中?是的,这是真的。您必须知道可执行文件的路径,无论是绝对路径还是相对路径。但这并不是崩溃。为什么不把
WPF
exe和Console
exe放在同一个目录中,或者像bin\myconsole.exe
那样的子目录中呢?创建新的进程时
,只需将控制台
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();
}
}
}
瞧。干杯,我想我会坚持到底的。:)