简体   繁体   中英

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). I want make Project 1 continuously running and every minute,I want Project 2 to launch, run, then close.

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 . We usually set it to ..\\Output\\Debug\\ for the Debug config, and ..\\Output\\Release\\ for the release config. This will put both of your projects in the same output directory, and they will be in the appropriate Debug/Release directories.

This way, you can reference the other .exe in the local directory context.

EDIT: Do this for both projects, and your output folder will be $(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.

Now right-click your secondary project, select Debug->Start New Instance, and it will also run side-by-side with your startup project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM