Windows Forms equivalent of WPFs OnStartup event - c#

protected override void OnStartup(StartupEventArgs e)
I want the equivalent of this event for Windows Forms.
I'm making a image viewing application. My application is one of the default programs that opens a .JPG. So how can i get the path of the file

In the file Program.cs, there should be following method:
static void Main(string[] args)
args contains the parameters passed to your application, e.g. the file that should be opened.

If you just need the path of the file started the process you can go about this
var path = Assembly.GetEntryAssembly().Location;
To get the path of file association clicked you need to look at the command line args
var pathOfFileAssociation = Environment.GetCommandLineArgs()[1] ;

You can do it like this:
private void Form1_Load(object sender, System.EventArgs e)
{
//this gives you the path of the executing assembly
MessageBox.Show(System.IO.Path.GetDirectoryName(Application.ExecutablePath));
}

Related

How to register a callback for an Activity Result with Xamarin Android

Background
I am developing an application that needs to browse a file manager , pick an image file and display that in an Android ImageView object. The code that starts the image file picker is this one below.
private void TextView_Click(object sender, EventArgs e)
{
//show the file manager with extensions
Intent image_intent= new Intent(Intent.ActionOpenDocument);
image_intent.AddCategory(Intent.CategoryOpenable);
//set the type for image files
image_intent.SetType("image/*");
StartActivityForResult(image_intent, 899);
}
Problem
However when I try to override the OnActivityResult() method so that I can process the image data from the resulting Intent data, Visual Studio IDE does not list the method OnActivityResult, it displays the output below.
What I tried
I tried to check if there is an existing override of this method but there was none.
I also tried to change the return type of the method to see if its been embedded in another return type but couldn't find it.
Why am I unable to override the method and how can I resolve this?
Looks like I was using the wrong access specifier to access the method, am supposed to use the keyword protected override void and not public override void. I had to check with the documentation on Microsoft Docs.

Launching a Console Applications from a MenuStrip (Visual 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).

Command line arguments in C# application

I have a WPF C# application, to which I have to pass command line argument. The argument is actually a URL, which I have to then use in my application?
How are these command line arguments passed in WPF C#, so that the application can pickup the url during launch?
In your App.xaml.cs
class App : Application
{
//Add this method override
protected override void OnStartup(StartupEventArgs e)
{
//e.Args is the string[] of command line arguments
}
}
It has been mentioned by linquize above, but I think it is worth an answer of its own, as it is so simple...
You can just use:
string[] args = Environment.GetCommandLineArgs();
That works anywhere in the application, not just in App.xaml.cs
You can pass arguments like "no-wpf" C# applications through comman line.
The difference is the application entry point. In WPF is App.xaml.cs. So, you have in this file you can pick arguments in this way:
class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
//e.Args represent string[] of no-wpf C# applications
}
}

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];
...
}
}

Starting an .exe from the same folder of my C# program

I made a small program in c# with a button that is supposed to open another .exe file.
It works fine if I use:
private void start_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start(#"path to file");
}
But not if I want it to run an .exe from the same folder, i basically wanted something like:
private void start_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start(#"program.exe");
}
What am I missing, I've tried a solution from this website:
var startIngo = new ProcessStartInfo();
startIngo.WorkingDirectory = // working directory
// set additional properties
Process proc = Process.Start(startIngo);
But Visual c# doesn't recognize "ProcessStartInfo" at all...
What your looking for is:
Application.StartupPath
It will return the startup path that your executable was started in.
If you are using WPF, try this instead:
String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
You can do:
var startupPath = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetEntryAssembly().Location);
var programPath = System.IO.Path.Combine(startupPath, "program.exe");
System.Diagnostics.Process.Start(programPath);
ProcessStartInfo is in the System.Diagnostics namespace - you need to import that namespace at the top of your cs file using a using System.Diagnostics; statement for the compiler to recognise ProcessStartInfo without specifying the namespace explicitly where you use the class.
You could also try System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
To get your local path. For example
//in your imports/using section
using System.IO
using System.Reflection
using System.Diagnostics;
//in your code to execute
Process.start(Path.GetDirectoryName(Aseembly.GetExecutingAssembly().GetName().CodeBase) + "\\program.exe")
There are two cases:
The application was started directly - start up path can be extracted from the command-line.
The application was started indirectly - e.g. from a unit-test, start up path can not be extracted from the command-line, however you can read it from the current directory into a static variable during the start-up (before the user has a chance to change it (e.g. using a file open/save dialog)).

Categories