How do I launch a file as if it were a text file, even though its extension is not .txt?

Raymond Chen

You might have a program that generates log files or other text content with an extension other than .txt. You naturally might want to open these documents in the user’s default text editor.

You might decide to ask the Windows developer support team, “How can I figure out what program is the handler for text files?” The idea being that once you get this program name, you can then run it yourself, with the document on the command line. And you would also be running into the trap of looking for the answer to a question rather than a solution to a problem.

For one thing, the default handler for the file type might require special command line parameters, parameters which you won’t get if you merely get the executable path. For example, on Windows 7, the default command line for JPG files is %SystemRoot%\System32\rundll32.exe "%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen %1, and if you merely asked for the executable, all you would get back would be rundll32.exe, and trying to execute rundll32.exe Boats.jpg doesn’t get you very far. You lost all the command line arguments.

For another thing, the default handler for the file type might not even be a command line. It might be an old program that uses DDE. Or the handler might be a drop target. Or it could be an IContext­Menu or an IExecute­Command. In these cases, there is no command line in the first place, so asking for the command line template is meaningless.

But we saw the answer to this question before, just in a different guise. The lpClass member of the SHELL­EXECUTE­INFO lets you open a file as if it were another type of file. In that article, somebody was passing a class when they didn’t mean to; here, we’re passing it on purpose.


Discussion is closed.

Feedback usabilla icon