Command line arguments in C# application - c#

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
}
}

Related

Obtaining the URI argument within a registered uap:protocol executable

I have a C# WPF application and I've referenced that inside a MSIX package.
I've also registered a URI protocol within the Package.appxmanifest of my MSIX package the following way:
<uap:Extension Category="windows.protocol" EntryPoint="AppLauncher"
Executable="AppLauncher\AppLauncher.exe">
<uap:Protocol Name="app-drive" >
</uap:Protocol>
</uap:Extension>
The goal is when I type app-drive:\\C:\test.txt into the file explorer or a browser for my executable to launch and within it I want to have the following string accessible C:\test.txt.
After I install my MSIX package and enter that string anywhere, my program launches correctly but that argument is nowhere to be found.
Ways I've tried to obtain it so far:
1.
[STAThread]
static void Main(string[] args)
{
// args[0] contains "-ServerName:App.AppXd****.mca" string
}
Environment.GetCommandLineArgs()
protected override void OnStartup(StartupEventArgs e)
All of these contain only the string "-ServerName:App.AppXd****.mca" as a first argument.
Note that I cannot use Windows.ApplicationModel.AppInstance.GetActivatedEventArgs() since my application is not UWP.
Any suggestions?
Edit:
Found the solution. Turns out in order for the parameter to be passed to the executable the EntryPoint has to be EntryPoint="Windows.FullTrustApplication"
Found the solution. Turns out in order for the parameter to be passed to the executable the EntryPoint has to be EntryPoint="Windows.FullTrustApplication".

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.

Windows Forms equivalent of WPFs OnStartup event

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));
}

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

How do I pass command line from a WPF application to another application?

Currently I'm running one WPF exe (which is in 3.5 framework) from another WPF application (4.0 application) by using below code:
var processInfo = new ProcessStartInfo("WPFAPP1.exe", "param");
Process.Start(processInfo);
Reason of the above is:
I'm using some third party which only support 3.5 framework and my existing application is on 4.0. I can't change my current application target type because it has around 200 projects that I would need to change then.
Now the problem is I want to get the command-line parameter (i.e "param" in my case) in the WPFAPP1.exe. For that I used the below piece of code in the WPFAPP1 application (the .exe application that I'm calling from the Master application),
public static String[] mArgs;
void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Length > 0)
{
mArgs = e.Args;
}
}
And in the MainWindow.xaml.cs:
public MainWindow()
{
InitializeComponent();
String[] args = App.mArgs;
txtTest.Text = args[0];
}
I tried the above code, but didn't get the respective value (that I passed from the Master application "param"). Also I didn't get any exception or error. Any help will really appreciated on this.
Take a look at System.Diagnostics.Process.GetCurrentProcess().StartInfo. It's a ProcessStartInfo instance that has an Arguments property that contains all the arguments of the current process.
You could create a new ProcessStartInfo instance and pass the arguments along to any sub-processes.

Categories