Running Projects Programmatically - c#

I was wondering if it is possible to open a second project (in the same solution as the first one) by code in the first project.
For example i have one form application project and another console application project.
The form application starts and when the user clicks a button i want the console application to run and the form application to stop.
Or could someone tell me how to delete my application .exe file?

The projects don't need to be in the same solution to do that. Just use Process.Start to start the executable for another application, and then close the main form to end the current application.
If you don't want to run the code as an entirely different process then it may also make sense to have a 3rd project that is a "class library" that the other two projects could add a reference to. This would allow you to define common code used in either application, using classes that are generalized to be helpful in either project.

I'm not sure what you're trying to do; but Process class has Start and Kill methods that will let you launch / exit processes.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

Related

Is it possible to open winform form from dependency?

I have a visual studio windows forms application that has multiple projects, each having multiple forms. The project that loads on startup calls another project's form and closes its own. Referring to the startup program as "setup" and the other as "main". I have a scenario where I want to skip setup and be able to return to it later. Since setup is a dependency of main, and not the other way around, I cannot seem to create an instance of the setup form. Is this the case, or am I doing something wrong?
The call from setup to open main is as so
this.Hide();
frmDemo demo = new frmDemo();
demo.ShowDialog();
this.Close();
I want to do the same thing from main to setup form, but I am having trouble.
Essentially it should just be
frmSetup setup = new frmSetup();
setup.show();
But this isn't working because it cannot find the form.
Well if you want to reference Setup from Main then you need to add a reference in Main.
The problem then is that you will create a circular dependency, which is bad design (and I think you will also get compiler errors).
There are some ways to get around this, but the best approach it to make your Main program the first one that starts-up (not setup). Then during startup it launches the set-up form and waits until it is finished and then continues. And then when it needs to launch setup again, then it is no problem.
So you are effectively reversing the dependency.

Console Application reading path from textbox WinForms (C#)

I have a console application that converts .txt files into .xml files. I made a WinForms application to make a menu and select a Console Application and run it. The user can choose the paths of the input files through a browse button, and it insert the paths as string into some textboxes. Now I want the Console App to read what they say so it can locate the input files.
Is there any way to do this?
It is pretty easy to launch a console app from a WinForms app by simply using a process. Example:
Process.Start("myConsoleApp.exe", "some arguments");
However, the only time this makes sense is when the console app is not your own code. Or if it is your own code, but it is a generic app that is called from different UIs or methods.
If none of the above is the case (which seems to be your case), it makes more sense to merge the UI and functionality in one app. You wanted the menu to be a generic app that calls different apps, so simply remove the UI related to the console app, make it a separate WinForms app, and add the code from the console app to it. Now your menu app will simply call a WinForms app that has the UI and functionality all in one. You will use the same method above, but likely you don't need to pass any arguments:
Process.Start("myWinformsApp.exe");
From your winform application you can launch another process (probably by passing exe path of console app you talking about and relevant i/p parameter of type string which is basically path of your text file provided by user)
This link might help you do that.

Multiple windows withing C# project

I want my project to have two executable windows. Whether that results in two executable files, or if both windows are launched when the exe is double clicked doesn't matter.
I want to be able to close the first window and have the second window continue to run. opening up a new instance of the first window shouldn't effect the second window, and no more than 1 instance of the second window should be allowed to run at once.
Ideally I would like my project to have two executable files, but I'm not sure how to implement this. I don't want to make them separate projects, because they share a lot of the same methods and variables, as well as user settings, but I may have to if that's the only way.
What is the best way to go about this?
Becuase they share so much I would suggest making 3 projects
1 for each of the windows
1 for the shared functionality.
This way, the shared functionality can be compiled into a separate DLL and can be used by both exe's

Run code once and exit formless app: Windows Forms or WPF... does it matter?

I need to write a very small application which writes some system data to a file and then exits. I could do this in a console application but I have no need or desire for a console window to appear during this process.
I would normally use a Windows Forms application with no forms, execute the code in the Main method and then allow the application to exit, however, this time I couldn't help but wonder if this is the best way to do it and whether you could do it with a WPF application instead, what the differences are and whether or not once you've remove any forms/windows and unnecessary reference, it matters or not.
WPF and WinForms are two different libraries that show UIs.
If you never show a UI, you aren't using either of them.
You should start with a WinForms project (WPF projects set extra project metadata that you don't want), then delete the reference to System.Windows.Forms.dll.
Alternatively, start with a console project, then change the Output type to Windows Application.
Windows Forms with no window or console app with the type changes to windows application will give you the same result which is a simple app with Main() method and now windows.
WCF will only make sense if you actually want to display something as you're not going to use any of its features in your case.

How can I run an application and do not show the console?

I've got a console application and I want to run it from another application. But when I invoke it from code--to call program I use ActiveXObject--it shows the console. How do I get it to not show the console? I mean, the program works but console is shown.
If you make the application a Windows Application instead of a Console Application it will not show or open (or use) a Console. Your code can stay the same - and the application will still function (without ever creating a Form or Window).
That being said, if you control the "other" application, you may want to consider making a class library instead of a separate Application. This would allow you to just include and run code within the library without starting a separate process.
You can create it as a Windows Forms application with a hidden window. That's just one possibility like Reed Copsey pointed out, i used it once because i needed to process some specific Window Messages.

Categories

Resources