After playing around with a new Windows Form project, I discovered that when you associate a file type with an executable in Windows, you can find the file path of the file that launched the application using args[0] from static void Main(string[] args)
Is it possible to launch an event on your application when you double click a file if your application is already open? (As obviously Main(string[] args) won't be triggered).
Example of an application with behavior I am attempting to replicate:
User Opens GIMP(in Windows)
User opens explorer and right clicks a .png file
User selects open with GIMP
Instead of creating a new application instance of GIMP, GIMP opens the picture in a new window within the instance of GIMP that was already opened.
In this case is GIMP employing multiple applications to accept files "opened" with file association? Or is it possible to do it with a single application "instance".
I'm having trouble with this as most of my searches tend to lead me towards file association as a Windows user (i.e. "How to associate .xls files with excel" articles).
There are a variety of options, but none of them come for free.
Your program's Main() can detect that there is another copy already running and hand the file name off to the already-running copy by some means you determine.
Your program can register as a DDE server and request that subsequent opens be performed via DDE. This is an old-fashioned technique from the 1990's that is generally not recommended for new programs.
You can register a custom drop target for your application, and have the drop target hand the file name to the already-running copy. This mechanism takes the form of a shell extension, and therefore is not suitable for C# due to CLR injection issues.
Given your constraints, option (1) appears to be the best option.
Raymond is right of course, but if you're looking for help with the implmentation of option (1) you should probably look at What is the correct way to create a single instance application? and .NET 4 single application instance and Switch to other instance of same application
You'll notice that detecting the application is pretty easy (use a mutex). Bringing the other application and sending it a filename can be more challenging.
There are three basic solutions presented in the answers to the previously linked questions
Use PostMessage to send a message to 1st instance. This uses HWND_BROADCAST which can have untended consequences.
Use Microsoft.VisualBasic.ApplicationServices.ApplicationBase Of course a reference to VisualBasic gives some C# devs the willies.
Use FindWindow which relies on a Windows Name.
Its also worth noting that if you want the existing application to be in the front you'll need to take special care because setting the foreground can only be given away not taken. See Foreground activation permission is like love: You can't steal it, it has to be given to you and AllowSetForegroundWindow and SetForegroundWindow
Microsoft created this functionality for Visual Basic .Net, and it can be used by C# too.
See the following post for a simple example in C#:
http://www.openwinforms.com/single_instance_application.html
This approach worked best for me: Enforcing Single Instance WPF Applications.
Especially, its solution for passing arguments also works in notify-icon only applications.
Related
I would like a particular type of file (eg. Namefile.ext2) read all the names preceded by a #
Sample contents of the file:
#nameone
#nametwo
#namethree
When I click the right mouse button on the ext2 file extension beyond the standard options (like: open, properties, etc ...) I would like to be:
contents of the file > nameone
nametwo
namethree
Then, select the item (eg. nameone) pass this parameter to my program running in the background / or services
Do you need to modify the registry somehow? I will be grateful for tips on how to achieve the desired effect.
What you are asking about is called 'shell extension'. Basically it requires some knowledge of COM objects programming, but .NET made things a bit easier in that matter.
Shortly: you have to develop a piece of code which will reads the file and generates menu items dynamically (which may be tricky but possible). That code needs to be registered in the system as COM object.
Before it starts working you have to associate file extension with COM object you created.
Perhaps this article can explaint it a bit more:
http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus
I don't know what it is called but I want to be able to double click on my saved file and then my program should open and load the file. What is this called and how do I do it?
I am using c# wpf and .net 4.0
BR
How about the last 2 fields, what am I supposed to write there?
That is a file association, if you want this to happen on a client machine you need to register your application as the default application for a given extension. This question might be handy.
To actually handle the opening you need to process the arguments that are handed to your application, they will contain the file path. You can get the arguments either in the override of Application.OnStartup (e.Args) or Environment.GetCommandLineArgs.
you need to register the file extension and associate it to your program, either during the setup using certain APIs or from code when program executes the first time.
check these ones:
How to associate a file extension to the current executable in C#
Associate File Extension with Application
personally I do not like the 100% registry approach, there should be some Windows APIs for that and we should let those APIs to work without worrying about the Registry from our side, in my opinion.
I'm implementing a small tool in C# which works on files.
Because I'm lazy I want to register my tool on the file types in the registry to be able to simply open all my files from the explorer.
Currently I am using HCR\MyFile\shell\open\command to specify the calling command line to my program with "%1" for the current file.
This works fine for single files, but oviously not when selecting multiple files.
I found out that my application should implement IDropTraget to get multiple files, but I don't know how to do this.
How to my CSharp "application" implement IDropTraget?
How to I then register my application in the windows registry for the file types correctly to get it called?
I am relatively new to the whole COM stuff. I "used" it several times now, but I never wrote a COM-class by my own.
Is there an example or tutorial? (I searched, but found nothing, at least I recognized nothing in this direction)
Thanks!
If your application uses Windows Forms, any class deriving from Control can set the AllowDrop property to true and handle D&D (almost) automatically.
See here for something similar: Drag and Drop files from Windows Explorer to Windows Form
Otherwise, you can still reuse Windows forms implementation at a lower level as IDropTarget is defined here: IDropTaget
I am trying to make a simple application which will be used to point a web browser control to some of our web applications at my work. I would like to have only one exe file but also have an admin window to change some of the settings and have them persist when the application is closed. Is that possible? I have looked at the application settings resources part but as I understand that makes a file that loads the settings.
I don't want to have to parse a file or have anything but ONE file so please don't suggest doing that if it is possible.
Just use application settings - that will create a single file, you won't have to do any parsing, it'll all be fine.
It'll be separate to the exe file, but unless you meant that "ONE file" to include the executable and rewrite that on the fly, it should fit your description easily.
For example, let's build a console app that just remembers how many times it's been launched:
Create a new console application project
Go to the properties page, and click into the Settings tab.
Click on the link to create a settings file
Type in the table to create a setting called "LaunchCount" of type int. Make it either user scope or application scope, depending on whether you want it to be persisted per user or system-wide.
Hit Ctrl-S to save.
In the Main method in Program.cs, write this code:
Settings settings = Settings.Default;
settings.LaunchCount++;
Console.WriteLine("Launch count: {0}", settings.LaunchCount);
settings.Save();
Add the appropriate using directive for Settings (put the cursor in Settings and hit Ctrl-.)
Run the app several times, and observe the number increasing.
You can't have persisted settings without having a separate file...safely. You must either have a separate file, which is the standard and suggested approach approach, like the one created with Application settings, or you must use something like the registry to save settings.
Keep in mind, though, that using the registry is highly discouraged due to security reasons. Plus most companies don't allow access to registries anyway which means that anyone without this access could not use the settings feature.
There are several ways to do this. You can use a command-line argument to do that. Launch the app from the shell and put in your command line argument and change how it launches.
A UNIX-y approach is to look at the name of the exe and change behavior based on that. If I recall correctly, rsh and rlogin are the same executable - they just look at argv[0] to decide how to run. In windows, this is straight forward - look at System.Environment.GetCommandLineArgs - if there is a non-empty string in the 0th element of that, it will be your executable name.
For persisting settings, see Jon Skeet's answer.
I have to say that this is generally a bad idea, but I've done this before a long time ago in VB6. I created a Resource within the exe and then (somehow) directly manipulated it.
The problem is, is that this is usually not possible within the .NET framework due to it being memory resident. These guys tryed it out in .net and they ended up creating an program in IL to do the heavy lifting... Modify Emdeded String in C# compiled exe
Go with a settings file as Jon suggested!
I am creating an application that uses a certain file format as its data source. I want this application to open whenever the user double clicks on this file, like how MS Word will open when a user double clicks on a Word document. How do I accomplish this? Also how would I populate the data fields using the file that the user selected. Would I use args[] from the program.cs class? I am using c# to code this application.
N.B. I want this association to be made when the application is installed on the host machine without the user doing anything.
FIRST, you need to set up file association, so that your file type is associated with your application and opening the file type will run your application.
You can do the file association programatically, there is some detail here as mentioned:
http://www.codeproject.com/KB/dotnet/System_File_Association.aspx
You can also do it via your Setup project for you application if you have one. This is an easier path for "newbies". Details for using visual studio to get the setup project to add the file association and also set the icon for the file are here:
http://www.dreamincode.net/forums/topic/58005-file-associations-in-visual-studio/
Otherwise if you use InnoSetup, Wix etc then I suppose you could just see instructions for those installers to create the association for you.
SECOND, you need to have your application accept command line arguments. The opened file(s) is(are) passed as a command line argument(s). You need to process the arguments to get the file path/name(s) and open the given file(s). There is a nice description of this here with code:
C# Command Line arguments problem in Release build
In your case, rather than MessageBox.Show(s) in the form shown handler, you would call your bespoke argument parsing method.
For a simple application which only accepts files names to open as arguments, this could be as simple as
foreach (string filePathName in Args)
DoNamedFileOpen(filePathName);
Your code can also have a method that might extract from the file the values for the datafields you are interested in etc.
This is a nice simple approach to the issue of have file associations set on installation of your application, with icons, and having your application handle the opening of those files.
Of course, there are plenty of other options, like run-time file association (asking the user if they want the association), detecting "broken" associations, etc.
This question is a long time here but I hope this is useful for new searches
See this. Or this if you want API information.
ClickOnce supports file associations as of .NET 3.5 SP1, too. In the project's properties, switch to the Publish tab and click the Options button. There's a File Associations section in that dialog that allows you to specify file extensions, descriptions and custom icons.
First, you have to associate the filetype extention with your executeable. On Windows you do this via the registry (search "filetype association windows"). In this question you find some interesting hints: Filetype association with application (C#) Script to associate an extension to a program
Your program has to react on the command line arguments or parameters. In Java, it is indeed the string array of the main method. I would gess, it's the same in C#.
If you don't need to do it pro programatically, right click on the icon, click open with ..., then select 'always use this program ...'.
This is something usually handled by your setup program .. I've used INNO setup for example, and it's trivially simple to arrange for it to adjust user's registry to launch your app when associated file extension is double clicked/opened. It'll even take care of MIME types for you as well as clearing these things on uninstall, which is a very nice thing to do
I managed to solve this issue. I used WIX to create an install file and asked it to associate the file with the application when it installs.