Launching a Console Applications from a MenuStrip (Visual C#) - c#

I want to have a Windows Form Application use a menustrip with three options to launch a console application. The console application is a .exe file built in C# in Visual Studio with some basic code for as school project. The console application does not need to return any values, it only needs to run and allow the user to use it. This is what the form will look like: Menu Application
I have tried importing the System.Diagnostics.Process.Start namespace with Process.Start#("Path of file") in my menu item click event method to launch my C# console application but have not been successful. I am getting a "Win32Exception was unhandled: An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll. Additional information: The system cannot find the file specified"
Here is the code in the menu item click event:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void lesson13LabCToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start(#"\C:\Users\Sam\Documents\Visual Studio 2015\Projects\LabMenu\LabMenu\Lesson13LabC.exe");
}
}
Any ideas on what I am doing wrong?

The error is clear The system cannot find the file specified. Check the path of file.
Also remove the starting \ in the path

Remove the backslash at the beginning of your path (before the drive letter).

Related

.Net 6.0 Windows App Cant Reference System

So the weirdness has hit. I created a new Visual Studio 2022 - fully patched, updated, and current.net 6.0 Windows Desktop app. It has 1 form that has a menu bar dragged from the toolbox on it and that is it. I have done nothing else to it! System, Form, void, object, EventArgs, and Application are all red and not available (see below)
using System;
namespace Developmeny_Test_Application
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
}
private void e7xitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
This is as basic as it gets and can I get Visual Studio to let it in?
Under Dependencies/COM it is showing an orange triangle (which I always thought meant depreciated but as this was created with the wizard templates that's not likely)
So my question is what am I missing? All the web searching I have done has revealed nothing of any use.
Any help is gratefully appreciated.
Added as an edit for more information
The section that has the triangle has this section in the project file :
<ItemGroup>
<COMReference Include="{bee4bfec-6683-3e67-9167-3c0cbc68f40a}">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>4</VersionMinor>
<VersionMajor>2</VersionMajor>
<Guid>bee4bfec-6683-3e67-9167-3c0cbc68f40a</Guid>
</COMReference>
</ItemGroup>
That COM-Reference is fishy. You should not need that. When I do the same (create a new Winforms project and add a "MenuStrip" to the form) I'm not getting that reference.
I'm assuming you accidentally added a component from a third-party library to your form, which caused this strange reference. Just delete that section from the project file and see what happens. If the error persists, please quote the exact error message you get.

Child Form cannot find text file

I have created a boolean algebraic simplifier. It simplifies expressions and I am content with it. However, I am trying to add a feature that allows users to check if two expressions are equivalent. For this I have created a new form that allows the user to input two expression by clicking buttons. To do this, I thought it best to simplify both expressions and then compare the two for equivalency. As I have got lots of subroutines and code that works for simplification in another form, I thought making the form a child form of the form with the code in would allow me to call the subroutines instead of copying them onto the form. I have made these protected in the parent form. I have inherited like so:
public partial class Expression_Equivalency_Form : Expression_Simplifier
However, when I click onto the form designer, this error appears and I cannot view the graphical interface of the form:
"Could not find file File Path"
The file is in the debug folder which is within the bin folder within the folder containing the program and is recongised in the parent class. The file is read from and appeneded by the parent form without issue. I have tried to research this but have been unable to find a solution. Does anyone know one?
I have read to the file and appended to it. I have also used the following code to remove any blank lines from my text file:
File.WriteAllLines("PreviousExpressionInputs.txt",
File.ReadAllLines("PreviousExpressionInputs.txt").Where(l => !string.IsNullOrWhiteSpace(l)));
Code that writes to the file:
using (BinaryWriter Writer = new BinaryWriter(File.Open("PreviousExpressionInputs.txt",
FileMode.Append)))
{
Writer.Write(expressionandanswertowritetotextfile);
}
Code that reads from the file:
foreach (string line in File.ReadLines("PreviousExpressionInputs.txt"))
{
try
{
LinesInFile.Add(line);
}
catch (Exception)
{
}
}
Consider following facts:
When you open a form in design mode, the constructor of its base class will run.
When you look for a relative file name, the path will be resolved relative to the current working directory of the application.
When the form is in design mode, the current application is Visual Studio and its working directory is where the devenv.exe is located.
It describes why you cannot find your text files. Because you have some code in the constructor of your base form(or fir example load event handler of the base form) which looks for the file and since the filename is relative, its looking for the file in the Visual Studio working directory and could not find file.
How to prevent the problem? Check DesignMode property to prevent running the code:
public partial class MyBaseForm : Form
{
public MyBaseForm()
{
InitializeComponent();
}
private void MyBaseForm_Load(object sender, EventArgs e)
{
MessageBox.Show("This will show both in run-time and design time.");
if (!DesignMode)
MessageBox.Show("This will show just in run-time");
}
}
Create the derived form and open it in designer to see what happens:
public partial class Form1 : MyBaseForm
{
public Form1()
{
InitializeComponent();
}
}
To learn more about how designer works take a look at this post.

Compile a Windows Forms application using csc.exe

In his book Erik Brown writes the following code
and compiles it from the command-line:
csc MyForm.cs
[assembly: System.Reflection.AssemblyVersion("1.1")]
namespace MyNamespace
{
public class MyForm : System.Windows.Forms.Form
{
public MyForm()
{
this.Text = "Hello Form";
}
public static void Main()
{
System.Windows.Forms.Application.Run(new MyForm());
}
}
}
I want to add another form and call it from the first.
Do I need a project file? An assembly file? I don't understand the build process. Can you explain the very basics to me: how do I tell the compiler to build a two-forms application?
First form (form1.cs):
public class MyForm : System.Windows.Forms.Form
{
public MyForm()
{
this.Text = "Hello Form";
this.Click += Form_Click;
}
public static void Main()
{
System.Windows.Forms.Application.Run(new MyForm());
}
private void Form_Click(object sender, System.EventArgs e)
{
MyForm2 form2 = new MyForm2();
form2.ShowDialog();
}
}
Second form (form2.cs):
public class MyForm2 : System.Windows.Forms.Form
{
public MyForm2()
{
this.Text = "Hello Form 2";
}
}
Now from the command line, locate to the location where you saved these .cs files and then run:
csc form1.cs form2.cs
It will create an EXE file. Run it and click in the form to open form2.
You need to use Visual Studio (VS) command prompt and mention the C# code file names (*.cs) of all the Windows forms in your project. You must include each form's code-behind file as well as designer code-behind file. Otherise, the compilation will not succeed.
A sample project structure is as below:
WindowsFormsApplication1.csproj
--Program.cs
--Form1.cs
----Form1.Designer.cs
--Form2.cs
----Form2.Designer.cs
The compilation command for above project will look like:
csc /target:winexe Program.cs Form1.cs Form1.Designer.cs Form2.cs Form2.Designer.cs
Note: It is compulsory to include Program.cs file during compilation, else the compiler fails to obtain the project's entry point(the Main method). The /target switch helps you to launch the output EXE file as a GUI based Windows Forms application.
There is a shorter version of the above command which uses wildcard to include all the files names in one go:
csc /target:winexe *.cs
The easiest alternative is to use the msbuild command in place of csc on Visual Studio command prompt. msbuild command can be used to build the project file which contains the reference of all the *.cs files. Here is how the command looks like:
msbuild WindowsFormsApplication1.csproj
This relieves us from mentioning all the C# code file names individually (whether in project root directory or nested sub-direcotries). Build the project file and you are done.

How do i get the file path document that is being open by my wpf application?

so i have a c# application that can save and open files. Windows explorer is registered to open that file format with my application. But how do i get the file path of the file that i double clicked in windows explorer?
you get it via commandline - on how to read those arguments see for example
http://msdn.microsoft.com/de-de/library/system.environment.getcommandlineargs.aspx
http://msdn.microsoft.com/en-us/library/aa970914.aspx
Your App.xaml.cs file should have an AppStartingUp method. The event args contain the command line arguments of the program. The first should be the file name:
public partial class app : Application
{
void AppStartingUp(object sender, StartupEventArgs e)
{
string file_name = e.Args[0];
...
}
}

COMException in System.WIndows.Forms.dll with Interactive Brokers TWS API

I've been trying to run the sample project provided with the tutorial at stockbotprogramming http://www.stockbotprogramming.com/sharpcibtutorial1.php , but I keep getting a COMException every time I run the application.
I have the TWS Client running and the sample VB projects provided with the API are able to connect just fine, but when I try to use the C# sample provided by the tutorial then I get the following exception:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in System.Windows.Forms.dll
Th exception happens when I try to add the TWS ActiveX control:
namespace CSharpTutorial1
{
public partial class Form1 : Form
{
private AxTWSLib.AxTws tws;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tws = new AxTWSLib.AxTws();
tws.BeginInit();
tws.Enabled = true;
tws.Location = new System.Drawing.Point(32, 664);
tws.Name = "tws";
Controls.Add(tws); // <-- EXCEPTION HERE!
tws.EndInit();
tws.connect("127.0.0.1", 7496, 0);
String msg = "Connected to TWS server version " + tws.serverVersion + "at " + tws.TwsConnectionTime;
MessageBox.Show(msg);
}
}
}
The original project was probably done with Visual Studio 2005, but I have Visual Studio 2008 and it automatically converted the project (I've been reading that there are some problems there). Does anybody know what could be causing this exception? Any ideas on how to fix it?
Important: Make sure you add the component to the IDE using the visual designer. First right click in the toolbox, click "choose items", click the COM tab, then check the TWS control. Now using the visual IDE drag this item to the surface of your form. It must be visible in your app. (Don't create it in the Form1_Load())
Do something like this... (don't use an IP Address of 127.0.0.1, leave it blank):
axTws1.connect("", 7496, 0);
axTws1.reqMktData(0, "AMD", "STK", "", 0, "", "", "SMART", "ISLAND", "USD", "", 0);
Last (if you're using 64-bit Windows) compile the project in 32-bit code. Go to the configuration manager and then create a new profile "x86" and set all the configurations to x86.

Categories