如何让Windows根据文件关联打开外部文件?

时间:2023-01-26 14:00:57

Using Win32-specific APIs, is there an easy way to start an external application to open a file simply by passing in the path/name of the file?

使用特定于Win32的API,是否有一种简单的方法可以通过传入文件的路径/名称来启动外部应用程序来打开文件?

For example, say I have a file called C:\tmp\image.jpg. Is there a single API that I can call to tell Windows to open the application associated with .jpg files? Without having to do a bunch of registry lookups?

例如,假设我有一个名为C:\ tmp \ image.jpg的文件。我可以调用一个API来告诉Windows打开与.jpg文件关联的应用程序吗?无需进行大量的注册表查找?

I thought I remembered doing this many years ago, but I cannot find it.

我以为我记得多年前这样做过,但我找不到它。

2 个解决方案

#1


12  

ShellExecute

Performs an operation on a specified file.

对指定文件执行操作。

Syntax

C++

HINSTANCE ShellExecute(
  _In_opt_ HWND    hwnd,
  _In_opt_ LPCTSTR lpOperation,
  _In_     LPCTSTR lpFile,
  _In_opt_ LPCTSTR lpParameters,
  _In_opt_ LPCTSTR lpDirectory,
  _In_     INT     nShowCmd
);

Parameters

...

nShowCmd [in]

Type: INT

The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it. These values are defined in Winuser.h...

指定应用程序打开时如何显示的标志。如果lpFile指定文档文件,则该标志将简单地传递给关联的应用程序。由应用程序决定如何处理它。这些值在Winuser.h中定义...

#2


6  

ShellExecute is the function you're looking for. It can handle both executable types and registered file types, and perform all sorts of actions (verbs) on the file, depending on what it supports.

ShellExecute是您正在寻找的功能。它可以处理可执行类型和已注册的文件类型,并根据它支持的内容对文件执行各种操作(动词)。

The syntax is:

语法是:

HINSTANCE ShellExecute(
    HWND hwnd,            // handle to owner window.
    LPCTSTR lpOperation,  // verb to do, e.g., edit, open, print.
    LPCTSTR lpFile,       // file to perform verb on.
    LPCTSTR lpParameters, // parameters if lpFile is executable, else NULL.
    LPCTSTR lpDirectory,  // working directory or NULL for current directory.
    INT nShowCmd          // window mode e.g., SW_HIDE, SW_SHOWNORMAL.
);

Consult your friendly neighborhood MSDN documentation for more details.

有关详细信息,请参阅友好邻居MSDN文档。

#1


12  

ShellExecute

Performs an operation on a specified file.

对指定文件执行操作。

Syntax

C++

HINSTANCE ShellExecute(
  _In_opt_ HWND    hwnd,
  _In_opt_ LPCTSTR lpOperation,
  _In_     LPCTSTR lpFile,
  _In_opt_ LPCTSTR lpParameters,
  _In_opt_ LPCTSTR lpDirectory,
  _In_     INT     nShowCmd
);

Parameters

...

nShowCmd [in]

Type: INT

The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it. These values are defined in Winuser.h...

指定应用程序打开时如何显示的标志。如果lpFile指定文档文件,则该标志将简单地传递给关联的应用程序。由应用程序决定如何处理它。这些值在Winuser.h中定义...

#2


6  

ShellExecute is the function you're looking for. It can handle both executable types and registered file types, and perform all sorts of actions (verbs) on the file, depending on what it supports.

ShellExecute是您正在寻找的功能。它可以处理可执行类型和已注册的文件类型,并根据它支持的内容对文件执行各种操作(动词)。

The syntax is:

语法是:

HINSTANCE ShellExecute(
    HWND hwnd,            // handle to owner window.
    LPCTSTR lpOperation,  // verb to do, e.g., edit, open, print.
    LPCTSTR lpFile,       // file to perform verb on.
    LPCTSTR lpParameters, // parameters if lpFile is executable, else NULL.
    LPCTSTR lpDirectory,  // working directory or NULL for current directory.
    INT nShowCmd          // window mode e.g., SW_HIDE, SW_SHOWNORMAL.
);

Consult your friendly neighborhood MSDN documentation for more details.

有关详细信息,请参阅友好邻居MSDN文档。