简体   繁体   中英

C# Winforms Application To Open a Second Winforms Application

I have an small application that I have written that needs to be used/incorporated into about 6 larger pieces. Ideally I want to be able to open this smaller piece from each stand-alone application instead of adding the code to each one. This would allow for easier maintenance in the event there are changes they only need to take place in one application.

I have looked but I can't seem to figure out how I can open a second winform application from inside of one. Any help would be great.

Thanks

2 options.

1) If this small app can stand alone, meaning it can execute in some useful fashion without being spawned by a parent app, then use Process.Start() . Your application(s) that spawn this process may not be able to run in a partial-trust environment.

Process.Start("notepad.exe");

2) If the app is only useful within the context of the parent apps, say by being given objects or other arguments from the other apps, consider refactoring the small app to allow the form's project to be referenced by the parent apps. Then, your parent apps can simply create a new instance of the form and display it:

var childForm = new SmallAppForm();
childForm.Owner = this; //the child window will close when its owner does.
childForm.Show();

Though it's more code, it's generally easier and more foolproof to implement. The form can also be accessed and thus controlled from elsewhere in the app, and other functionality can be implemented without having to deal with the rather messy business of cross-process communication.

I'd look to make this a Library that's a separate project that you add a reference to from your other projects. Much cleaner than spawning child processes.

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