This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# Windows 'Open With >' Context menu behaviour
How do I do this? Like if I right click on a file and click open with, then my program how do I do stuff to that file :/.
I use the following code to pass the first argument (the one that contains the file name) to my gui application:
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(args.Length == 0 ? new Form1(string.Empty) : new Form1(args[0]));
}
}
I test to see if there is an argument. If not and a user starts your program without one then you may get an exception in any code that tries to use it.
This is a snippet from my Form1 that deals with the incoming file:
public Form1(string path) {
InitializeComponent();
if (path != string.Empty && Path.GetExtension(path).ToLower() != ".bgl") {
//Do whatever
} else {
MessageBox.Show("Dropped File is not Bgl File","File Type Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
path = string.Empty;
}
//.......
}
You will see that I am checking the extension sent in - my app only works with one extension type - .bgl - so if the user tries to open some other file extension then I stop them. In this case I am dealing with a dropped file. This code will also allow a user to drag a file over my executable (or related icon) and the program will execute wit hthat file
You might also consider creating a file association between your file extension and your program if you have not already. This in conjunction with the above will allow the user to double click on your file and have your application open it.
The path to the file being opened is passed as a command line argument to your application. You need to read that argument and open the file from that path.
There are two ways to do this:
The easiest way is to loop through the values of the args array passed as the single parameter to the Main method:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
The second way to do it is to use the Environment.GetCommandLineArgs method. This is useful if you want to extract the arguments at some arbitrary point within your application, rather than inside of the Main method.
Related
This question already has answers here:
No Main() in WPF?
(7 answers)
Closed 1 year ago.
Usually this is done by simply adding [STAThread] on the Main method - However with new projects there is none of that?
When i try to set my own program entry point like this:
class Program
{
[STAThread]
static void Main(string[] args)
{
var application = new App();
application.Run();
}
}
it says that there is more than one program entries found. So yeah - with old .net wpf this worked, how to do it with netcore3/net5+ ?
You don't need to - the automatically generated Main method already has that.
You can find the generated files in the appropriate obj directory. For example, I just ran dotnet new wpf from a WpfSta directory and this was generated as part of obj/Debug/net5.0-windows/App.g.cs:
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "5.0.6.0")]
public static void Main() {
WpfSta.App app = new WpfSta.App();
app.InitializeComponent();
app.Run();
}
You can write your own Main method, and then use the StartupObject property in your project file to specify the entry point. But if it's just for adding STAThread, you don't need to.
I've written a custom exe. Files of a sepcific type can be openend with this exe (properties -> open with). Now, in this exe's source code I'd like to access the file which ran the exe (e.g. the path to the file). Is this possible?
I'm guessing you want the path from where the explorer opened the file; in normal cases this will be passed to your program as an argument (like "C:\\myapplication.exe filename")
Modify your Main-method (program entry) from
[STAThread]
static void Main()
{
to
[STAThread]
static void Main(string[] args)
{
If the args collection contains something, that is the passed filename
[STAThread]
static void Main(string[] args)
{
if (args.Length != 0)
{
string fileName = args[0]; // something like this
When you use "Open with" in Explorer for a file or simply double-click on a file if it's permanently assigned to a program, the path of the file is passed to the program via command line arguments. So you have to examine the command line arguments in your program to determine if it was called with any arguments that could be a file to open.
See here for example: How to access command line parameters outside of Main in C#
Pretty much exactly what the title says. I created an exe windows form program in C# and i found how to obtain the file name and path that was dragged and dropped onto the exe. Well is this the proper way to obtain the file?
public partial class Form1 : Form
{
public static String file;
public Form1()
{
foreach (String arg in Environment.GetCommandLineArgs())
{
file = arg;
}
InitializeComponent();
label1.Text = file;
}
}
}
It does work and if i run the program itself, it gives me the file path and name of the exe itself. But when dragging and dropping to the exe, it gives me the file that i dropped. Is this the proper way to do it?
In general, GetCommandLineArgs may or may not contain path to executable as first argument. But in most cases - first argument will be your exe.
So, second, third and so on arguments will contains dropped files.
Hm, thought command line passed to the main method of main class in C#, no?
I have a C# WinForm App that I created to store files in a seperate secure location on the hard drive. I am trying to add functionality to the program by adding a right-click context menu so when a user right-clicks a file (or group of files) in windows, my program is there in the context for them to select. No problem there, I have that part worked out. What I need is to programmatically get that list of files and send it to the program so they are listed in the listbox already.
I am already doing something similar with a multiselect in an OFD, but I dont want them to have to open the program, select browse, find the files and select them when they already have them selected in windows.
There are a ton of programs out that have this functionality (like properties plus, textpad, etc...) I just need a shove in the right direction to help me figure this out.
Thanks in advance,
Dave
If I'm correctly understanding what you've already implemented, then all the files should appear as arguments on the program's command line. You just need a way of extracting each of those file paths and displaying them in your list view.
In C#, the following code will display a message box containing each argument on the command line:
static void Main(string[] args)
{
foreach(string arg in args)
{
MessageBox.Show(arg);
}
}
But in case you don't want to access these in the Main method, you can also use the Environment class, which provides the static GetCommandLineArgs method. It returns the same array of strings containing the arguments, and you can loop through it the same way.
Here is an article on how to customise Right-Click Menu Options in Windows
Then just as #CodyGray says use the string[] args in your Main method of you program to get the filenames
I am gathering all the arguments and sending them to an ArrayList.
static void Main(string[] args)
{
ArrayList myAL = new ArrayList();
foreach (string arg in args)
{
myAL.Add(arg);
}
ALRec nalr = new ALRec();
nalr.getArrList(myAL);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
sending it to ALRec Class
class ALRec
{
ArrayList MyArrLst = new ArrayList();
public void getArrList(ArrayList AL)
{
MyArrLst = AL;
}
}
Why is it starting multiple instances of my App?
I am working on a program that can read, write, and export files, these functions all work fine and are almost perfected. What I would like to do now is to be able to choose a file and tell it to "Open With" (In the Right-Click Context Menu on Windows XP) and have my application be able to handle the file given. I have no idea on where to start or where to look so I thought I'd ask here. Thanks.
You might take a look at this Windows KB article:
"How To Associate a File Extension with Your Application (Win32)"
http://support.microsoft.com/kb/185453
It looks like it gives example code for how to do this in VBScript (?), but it looks like it goes through the Registry paths you need to look at.
Hey, I believe this is defined in the registry. E.g. MSAccess is defined as:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Access.Application.11\shell\Edit]
#="&Edit"
[HKEY_CLASSES_ROOT\Access.Application.11\shell\Edit\command]
#="\"C:\\Programmer\\Microsoft Office\\OFFICE11\\MSACCESS.EXE\" /NOSTARTUP \"%1\""
[HKEY_CLASSES_ROOT\Access.Application.11\shell\Edit\ddeexec]
#="[SetForeground][ShellOpenDatabase \"%1\"]"
[HKEY_CLASSES_ROOT\Access.Application.11\shell\Edit\ddeexec\Application]
#="Msaccess"
[HKEY_CLASSES_ROOT\Access.Application.11\shell\Edit\ddeexec\IfExec]
#="[SHELLNOOP]"
[HKEY_CLASSES_ROOT\Access.Application.11\shell\Edit\ddeexec\Topic]
#="ShellSystem"
A GUI also exists in Folder settings -> File types.
Br. Morten
Bring up the run dialog box, and enter: regedit (Registry Editor)
Go to: HKEY_CLASSES_ROOT\*\shell and create a subkey named: "Open With YourApp", create another subkey under the newly created one named "command". On its Default value, enter the path to your exe, then add "%1" at the end for the parameter.
In program.cs, add the indicated lines below:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var mainForm = new MainForm();
// Add these lines:
// ----------------------------------------------
string[] args = Environment.GetCommandLineArgs();
if (args.Count() >= 2)
mainForm.LoadFile(args[1]);
// ----------------------------------------------
Application.Run(mainForm);
}
}
Where LoadFile(string filePath) is your method that handles the file that is passed in from outside.