Question How do you read a file on opening

FroDesigns

Member
Joined
Aug 23, 2009
Messages
6
Programming Experience
1-3
When you open a file with a program like: right click->open with...->program
How do you get the program to read the file?
 
When the user opens a file that way the file path gets passed to the selected application as a commandline argument. For instance, if you were to right-click on the file C:\Test.txt and select Open With -> Notepad, it would be equivalent to select Start -> Run and typing "notepad c:\test.txt" (without quotes).

In your application you would generally retrieve commandline arguments using the Environment.GetCommandLineArgs method. It returns an array where each element is an argument. The first is always the name/path of the application EXE itself. Any beyond that were passed to the app when it was run. You can test that array for length first to see if there are any commandline arguments. If there are you can get them and process them appropriately. In your case that would involve calling File.Exists to make sure that it's a valid file, then opening the file in whatever way is appropriate for its content.
 
Back
Top