opening file with my program
-
I asked this earlier, and Stefan Troschtz gave me an answer (thanks), but it wasn't what i was looking for and I think it was because i didn't explain it well enough. What code should i have in my program so that when a process is started of a file, and it is the type that my program opens, my program opens it? ex. when you start a process for a website, internet explorer opens it, when you open a .psd Photoshop opens it. Basically I am asking how i should pass the filename to my program so that it opens the file it should be opening. Or if that is not what you would do, what would you do? Thanks again. :)
-
I asked this earlier, and Stefan Troschtz gave me an answer (thanks), but it wasn't what i was looking for and I think it was because i didn't explain it well enough. What code should i have in my program so that when a process is started of a file, and it is the type that my program opens, my program opens it? ex. when you start a process for a website, internet explorer opens it, when you open a .psd Photoshop opens it. Basically I am asking how i should pass the filename to my program so that it opens the file it should be opening. Or if that is not what you would do, what would you do? Thanks again. :)
The first article really wasn't what you're looking for, but I hope the following will help you :) System File Association[^] Associate File Extension with Shell OPEN command and Application[^]
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
I asked this earlier, and Stefan Troschtz gave me an answer (thanks), but it wasn't what i was looking for and I think it was because i didn't explain it well enough. What code should i have in my program so that when a process is started of a file, and it is the type that my program opens, my program opens it? ex. when you start a process for a website, internet explorer opens it, when you open a .psd Photoshop opens it. Basically I am asking how i should pass the filename to my program so that it opens the file it should be opening. Or if that is not what you would do, what would you do? Thanks again. :)
Firstly, it would have been better to keep this to the original thread where others could benifit from knowing what advice you've already been given. Secondly, your application would need to process command line arguments. As an example, if you had a text file named "myfile.foo" in the same directory as you appliation, you would be able to pass the filename to your application at the command prompt by typing "myApp.exe myfile.foo" or alternatively drag & drop the Foo file onto your exe. This would start your application which could then perform any function you like. It would obviously make sense that you application check the file extension ".foo" was correct before doing anything. For more information on command line arguments, look here: http://msdn2.microsoft.com/en-us/library/acy3edy3.aspx You could rig up a test application? If you create a new console application, you can paste the following code to replace the Main method:
public static void Main(string[] args) { if ( args.Length > 0 ) { if ( args[0].ToLower().EndsWith(".foo") ) //do something with foo file Console.WriteLine( "Openning Foofile named {0}", args[0] ); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
If you then build the application and create a text file named "test.foo" in the same folder, you should be able to drag & drop the Foo file onto your app. This should run the app which simply displays the full path to the Foo file. Once you've got this working, you've just got to create some method to actually do something with the file. I guess it depends what you want it to do? Lastly, you would have to associate an file extension with your application so that Windows knows what to do when somone tries to open this type of file. This is normally the job of an installer like InstallShield however, you can make this association manually. If you double-click the Foo file, windows will normally complain that it cant open the file, then.. 1) Simply select "Select a program from the list" and click "OK" 2) Tick the box which reads "Always use the selected program to open this kind of file" 3) The click the "browse" button where you can search for your application. Foo files would always be opened by your application. Hope this helps
Regards Wayne Phipps ______
-
Firstly, it would have been better to keep this to the original thread where others could benifit from knowing what advice you've already been given. Secondly, your application would need to process command line arguments. As an example, if you had a text file named "myfile.foo" in the same directory as you appliation, you would be able to pass the filename to your application at the command prompt by typing "myApp.exe myfile.foo" or alternatively drag & drop the Foo file onto your exe. This would start your application which could then perform any function you like. It would obviously make sense that you application check the file extension ".foo" was correct before doing anything. For more information on command line arguments, look here: http://msdn2.microsoft.com/en-us/library/acy3edy3.aspx You could rig up a test application? If you create a new console application, you can paste the following code to replace the Main method:
public static void Main(string[] args) { if ( args.Length > 0 ) { if ( args[0].ToLower().EndsWith(".foo") ) //do something with foo file Console.WriteLine( "Openning Foofile named {0}", args[0] ); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
If you then build the application and create a text file named "test.foo" in the same folder, you should be able to drag & drop the Foo file onto your app. This should run the app which simply displays the full path to the Foo file. Once you've got this working, you've just got to create some method to actually do something with the file. I guess it depends what you want it to do? Lastly, you would have to associate an file extension with your application so that Windows knows what to do when somone tries to open this type of file. This is normally the job of an installer like InstallShield however, you can make this association manually. If you double-click the Foo file, windows will normally complain that it cant open the file, then.. 1) Simply select "Select a program from the list" and click "OK" 2) Tick the box which reads "Always use the selected program to open this kind of file" 3) The click the "browse" button where you can search for your application. Foo files would always be opened by your application. Hope this helps
Regards Wayne Phipps ______
Hello, and thanks. I can get the command prompt to work pretty well, but now I want a more graphical program. I have tried changing the code that you suggested and the code that I found at MSDN online to fit the Windows Interface template in Visual Studio 2005, but I have not been successful. I have tried searching some more for the solution, but have not found anything more about the Main(). So if you know of a website that would be helpful in creating what i am looking for, or if you have a quick simple solution, that would be very helpful. Thanks again.
-
Hello, and thanks. I can get the command prompt to work pretty well, but now I want a more graphical program. I have tried changing the code that you suggested and the code that I found at MSDN online to fit the Windows Interface template in Visual Studio 2005, but I have not been successful. I have tried searching some more for the solution, but have not found anything more about the Main(). So if you know of a website that would be helpful in creating what i am looking for, or if you have a quick simple solution, that would be very helpful. Thanks again.
Ok, the pointer should have been enough to get the ball rolling. It showed one way to obtain the file name passed as a command line argument. What you do from here on is up to you. Windows Forms applications pass command line arguments in the same way, the only difference is how you reference the arguments. One way would be to use a member variable to hold the filename. If you make this a
public static
you can then access if from anywhere within your application. You could do something as follows:public static string FooFileName = null; [STAThread] public static void Main(string[] args) { //check for the required file extension if ( args.Length > 0 && args[0].ToLower().EndsWith(".foo") ) { //update the static member withe the file name FooFileName = args[0]; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); //check that the filename is not null if ( FooFileName != null ) { //do something with the file MessageBox.Show( String.Format("FooFile Name: {0}", FooFileName) ); } }
Another way would be to create a constructor which allows the filename to be passed as follows:
[STAThread] public static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if ( args.Length > 0 && args[0].ToLower().EndsWith(".foo") ) { //pass the the FooFile name to the MainForm constructor Application.Run(new MainForm( args[0] )); } else { //pass nothing to the MainForm constructor Application.Run(new MainForm( null )); } } public MainForm( string FooFileName ) { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // //do something if a FooFileName was passed if ( FooFileName != null ) MessageBox.Show( String.Format("FooFile name: {0}", FooFileName) ); }
Does that help?? If so maybe you would consider rating my answer? If not maybe we should consider further discussions via email.
Regards Wayne Phipp