[英]How can I run a project that is in the same solution?
I have two projects in one solution. 一个解决方案中有两个项目。 What I want to do is have one project as a main project(Project 1) and every now and then it will launch the other project(Project 2).
我要做的是将一个项目作为主要项目(Project 1),然后不时地启动另一个项目(Project 2)。 I want make Project 1 continuously running and every minute,I want Project 2 to launch, run, then close.
我希望Project 1持续运行并每分钟运行一次,我希望Project 2启动,运行然后关闭。
Here is what I have tried so far: 到目前为止,这是我尝试过的:
if (CurrentTime.Minute % 2 == 1 && CurrentTime.Second == 30 && CurrentTime.Millisecond < 5)
{
try
{
string ThisDir = Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\Project2\\bin\\Debug\\Project2.exe";
Process.Start(ThisDir);
}
catch
{
}
}
The problem is that if I want to run it in release this doesn't work. 问题是,如果我想在发行版中运行它,将无法正常工作。
Another note is that because I don't want this always running, I don't want to set it as a starting project in the solution properties. 另一个要注意的是,因为我不想让它一直运行,所以我不想在解决方案属性中将其设置为启动项目。 Is there a better way of starting another project that is in the same solution?
是否有更好的方法启动相同解决方案中的另一个项目?
I can't really think of a "pretty" way of doing this off the top of my head. 我真的想不出一种“漂亮”的方法来做到这一点。 I guess you could use preprocessor directives to determine the path if you want a quick and dirty solution.
我想如果需要快速而肮脏的解决方案,可以使用预处理程序指令来确定路径。 Something like
就像是
#if DEBUG
string ThisDir = Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\Project2\\bin\\Debug\\Project2.exe";
#else
string ThisDir = Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\Project2\\bin\\Release\\Project2.exe";
#endif
What we've done in the past is Properties
-> Build Tab
-> Output path
. 我们过去所做的是
Properties
> Build Tab
> Output path
。 We usually set it to ..\\Output\\Debug\\
for the Debug config, and ..\\Output\\Release\\
for the release config. 对于调试配置,我们通常将其设置为
..\\Output\\Debug\\
,对于发布配置,我们将其设置为..\\Output\\Release\\
。 This will put both of your projects in the same output directory, and they will be in the appropriate Debug/Release directories. 这会将您的两个项目放在相同的输出目录中,并且它们将在相应的Debug / Release目录中。
This way, you can reference the other .exe in the local directory context. 这样,您可以在本地目录上下文中引用另一个.exe。
EDIT: Do this for both projects, and your output folder will be $(SOLUTION_DIR)\\Output\\Debug\\ 编辑:对两个项目都执行此操作,并且您的输出文件夹将是$(SOLUTION_DIR)\\ Output \\ Debug \\
Then, you simply have to call: 然后,您只需调用:
Process.Start("Project2.exe");
I don't quite understand why you want that. 我不太明白你为什么要那样。 But something I only learnt recently is that you can start multiple projects at the same time.
但是我最近才了解到的一点是,您可以同时启动多个项目。
eg press F5, and the startup project starts running. 例如,按F5键,启动项目开始运行。
Now right-click your secondary project, select Debug->Start New Instance, and it will also run side-by-side with your startup project. 现在,右键单击您的辅助项目,选择“调试”->“启动新实例”,它还将与启动项目并排运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.