I am building a windows forms application using C# that needs to get launched when a user clicks on a file with custom extension(eg. filename.mycustomextension) I plan to put a url in the filename.mycustomextension file. when user clicks on this file, we winform application should launch and also read contents of this file. Is it possible to do this?
First and most obviously you'll need to associate the file extension with the application either by "open with" in the shell, through an installer or directly in the registry.
MSDN - Best Practices for File Associations
Then from there it's really pretty simple.
static class Program
{
[STAThread]
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
string text = File.ReadAllText(args[1]);
// ...
}
}
Offhand I can't find any examples that show all of this together simply but Scott Hanselman has a nice example of loading files through a single instance WinForms application, about the same...
http://www.hanselman.com/blog/CommentView.aspx?guid=d2f676ea-025b-4fd6-ae79-80b04a34f24c
Yes, it is possible.
The idea is that when your application is clicked, you modify the registry key, to associate the extension file with your application.
Here are the sketches:
FileAssociation
class from here.Here is an example:
var FA = new FileAssociation();
FA.Extension = "blah";
FA.ContentType = "application/blah";
FA.FullName = "blah Project File";
FA.ProperName = "blahFile";
FA.AddCommand("open", string.Format("\"{0}\" \"%1\"", System.Reflection.Assembly.GetExecutingAssembly().Location));
//"C:\\mydir\\myprog.exe %1");
FA.IconPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
FA.IconIndex = 0;
FA.Create();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With