How to Open a File with the Default Associated Program

How to open a file with the default associated program

You can use Desktop.getDesktop().open(File file). See the following question for other options: "[Java] How to open user system preffered editor for given file?"

Open file with default associated program using Java/C++

With C++ should be this: ShellExecute(0, 0, L"document.doc", 0, 0 , SW_SHOW );

Java: java.awt.Desktop.getDesktop().open("document.doc");

How to open file with default application in cmd?

You can use start to open the file with the associated application.


Resources :

  • Open a File in the Default Application using the Windows Command Line (without JDIC) (waybackmachine capture from Oct 30, 2010)

c# open file with default application and parameters

If you want the file to be opened with the default application, I mean without specifying Acrobat or Reader, you can't open the file in the specified page.

On the other hand, if you are Ok with specifying Acrobat or Reader, keep reading:


You can do it without telling the full Acrobat path, like this:

using Process myProcess = new Process();    
myProcess.StartInfo.FileName = "acroRd32.exe"; //not the full application path
myProcess.StartInfo.Arguments = "/A \"page=2=OpenActions\" C:\\example.pdf";
myProcess.Start();

If you don't want the pdf to open with Reader but with Acrobat, chage the second line like this:

myProcess.StartInfo.FileName = "Acrobat.exe";

You can query the registry to identify the default application to open pdf files and then define FileName on your process's StartInfo accordingly.

Follow this question for details on doing that: Finding the default application for opening a particular file type on Windows

How to open a file in its default program with python

It's hard to be certain from your question as it stands, but I bet your problem is backslashes.

[EDITED to add:] Or actually maybe it's something simpler. Did you put quotes around your pathname at all? If not, that will certainly not work -- but once you do, you will find that then you need the rest of what I've written below.

In a Windows filesystem, the backslash \ is the standard way to separate directories.

In a Python string literal, the backslash \ is used for putting things into the string that would otherwise be difficult to enter. For instance, if you are writing a single-quoted string and you want a single quote in it, you can do this: 'don\'t'. Or if you want a newline character, you can do this: 'First line.\nSecond line.'

So if you take a Windows pathname and plug it into Python like this:

os.startfile('C:\foo\bar\baz')

then the string actually passed to os.startfile will not contain those backslashes; it will contain a form-feed character (from the \f) and two backspace characters (from the \bs), which is not what you want at all.

You can deal with this in three ways.

  • You can use forward slashes instead of backslashes. Although Windows prefers backslashes in its user interface, forward slashes work too, and they don't have special meaning in Python string literals.

  • You can "escape" the backslashes: two backslashes in a row mean an actual backslash. os.startfile('C:\\foo\\bar\\baz')

  • You can use a "raw string literal". Put an r before the opening single or double quotes. This will make backslashes not get interpreted specially. os.startfile(r'C:\foo\bar\baz')

The last is maybe the nicest, except for one annoying quirk: backslash-quote is still special in a raw string literal so that you can still say 'don\'t', which means you can't end a raw string literal with a backslash.



Related Topics



Leave a reply



Submit