windows 关联文件类型和图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1903702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:35:14  来源:igfitidea点击:

Associate File Type and Icon

c#windowsfile-association

提问by Patrick

Ok, I have looked all over and I am having a difficult time trying to figure out how to associate a custom file type with my C# application. I am using Visual Studio 2008 and am limited to .NET 2.0 due to company deployment.

好的,我已经查看了所有内容,但我很难弄清楚如何将自定义文件类型与我的 C# 应用程序相关联。我正在使用 Visual Studio 2008 并且由于公司部署而仅限于 .NET 2.0。

Basically all I really want to do is be able to allow the user to double click my custom file type or drag and drop it on the app icon and it load the data, as well as display a custom icon on the file itself. I can open the file via a dialog box and everything works great.

基本上我真正想要做的就是允许用户双击我的自定义文件类型或将其拖放到应用程序图标上并加载数据,以及在文件本身上显示自定义图标。我可以通过对话框打开文件,一切正常。

Trying to read Micosoft's online documents is hard to understand and does not really give me all the details that I need. If anyone out there know's where I can find a good tutorial or can explain it I would appreciate it.

尝试阅读 Micosoft 的在线文档很难理解,并且并没有真正给我我需要的所有细节。如果有人知道我可以在哪里找到好的教程或可以解释它,我将不胜感激。

Thank you Patrick

谢谢帕特里克

采纳答案by Daniel Brückner

This CodeProject articlehas some source code demonstrating file association by code.

这篇CodeProject 文章有一些源代码,通过代码演示文件关联。

回答by P Daddy

I wrote this upin the newsgroups a few years ago. Skimming through it, I find that it's not the clearest I've ever written, but it's fairly complete. For what it's worth, I've reprinted it below:

几年前我在新闻组里写过这个。浏览它,我发现它不是我写过的最清晰的,但它相当完整。为了它的价值,我在下面转载了它:



First you need to create a subkey under HKEY_CLASSES_ROOTthat will hold the commands that start your application. You should name this key something semi-descriptive. Your file extension[s] will be mapped to this key. For example, TXT files are mapped to a key named txtfile, by default. The benefit of using this setup is that if you have multiple extensions that your app can handle, you can map them all to this key. For example, many image editting apps create a subkey called something like "imagefile", and map .bmp, .jpg, .gif, etc. to that key. We'll call your key "JoeBlowFile". Next, you need to set the "default" value for your new JoeBlowFile key to a text string describing to the user just what type of file they have. This is what shows up in Windows Explorer under "Type". Again, to use the TXT file example, a good string here would be "Text File" or "Text Document". (It is the latter by default.) Your string might be "Joe Blow Data".

首先,您需要在其下创建一个子项,HKEY_CLASSES_ROOT该子项将保存启动应用程序的命令。您应该将此键命名为半描述性的。您的文件扩展名 [s] 将映射到此键。例如,TXT 文件被映射到一个名为txtfile, 默认情况下。使用此设置的好处是,如果您的应用程序可以处理多个扩展,则可以将它们全部映射到此键。例如,许多图像编辑应用程序会创建一个名为“imagefile”之类的子键,并将 .bmp、.jpg、.gif 等映射到该键。我们将您的密钥称为“JoeBlowFile”。接下来,您需要将新 JoeBlowFile 键的“默认”值设置为向用户描述他们拥有的文件类型的文本字符串。这是 Windows 资源管理器中“类型”下显示的内容。同样,要使用 TXT 文件示例,这里的一个好的字符串应该是“文本文件”或“文本文档”。(默认情况下是后者。)您的字符串可能是“Joe Blow Data”。

Now, under your new key, you can create another subkey, called "DefaultIcon". This, as its name suggests, sets the icon that is used with files of this type. You should create a custom icon that pictorially represents documents handled by your program. You can save this icon as an ICO file in your app's directory, but even better, you can include it as a resource in either your EXE or DLL. Either way, you'll then need to set the subkey's default value to a string representing the full path and filename of the ICO, EXE, or DLL. If there is more than one icon in the file (particularly likely if you include it as a resource in your EXE or DLL), you'll also need to add a comma, and either a zero-based positive index number representing which icon you'd like to use, or a negative resource ID, using the negative of whatever ID you've assigned your icon in your resource script. So yours could be, for example "C:\Program Files\JoeBlow\JoeBlow.exe, 2".

现在,在您的新项下,您可以创建另一个子项,称为“DefaultIcon”。顾名思义,这会设置用于此类文件的图标。您应该创建一个自定义图标,以图形方式表示由您的程序处理的文档。您可以将此图标保存为应用程序目录中的 ICO 文件,但更好的是,您可以将其作为资源包含在 EXE 或 DLL 中。无论哪种方式,您都需要将子项的默认值设置为表示 ICO、EXE 或 DLL 的完整路径和文件名的字符串。如果文件中有多个图标(尤其是当您将其作为资源包含在 EXE 或 DLL 中时),您还需要添加一个逗号,以及一个从零开始的正索引号,表示您使用的是哪个图标想使用,或负资源 ID,使用您在资源脚本中为图标分配的任何 ID 的否定。所以你的可能是,例如“C:\Program Files\JoeBlow\JoeBlow.exe, 2”。

A note for C# developers on the above paragraph. Unfortunately, C# projects can't have resource scripts. Resources added to .NET applications by adding them to the project and designating them an "Embedded Resource" are included in a new .NET-specific format that's not compatible with previous methods. The only icon you can correctly embed in your application using C# on VS.NET is the application icon, accessible from the project properties. If you need additional icons—e.g. an icon to represent a document file handled by your app rather than to represent the app itself—you'll need to either include the ICO files themselves, compile a DLL with C++ with your icons embedded, or create and compile a resource script and include it in your project from the project properties.

C# 开发人员对上一段的说明。不幸的是,C# 项目不能有资源脚本。通过将资源添加到项目并指定“嵌入式资源”来添加到 .NET 应用程序的资源包含在新的 .NET 特定格式中,该格式与以前的方法不兼容。在 VS.NET 上使用 C# 可以正确嵌入到应用程序中的唯一图标是应用程序图标,可从项目属性访问。如果你需要额外的图标——例如代表你的应用程序处理的文档文件而不是代表应用程序本身的图标——你需要包含 ICO 文件本身,用 C++ 编译一个嵌入了你的图标的 DLL,或者创建并编译资源脚本并将其从项目属性中包含在您的项目中。

Whether or not you choose to use the DefaultIconkey, you now need to create a subkey named "shell" under your JoeBlowFilekey. Under the shellkey, you'll create individual keys for each of the commands you'd like the user to be able to perform on your file type from the context menu (right-click menu). These items are called "verbs". For consistency, one of them should be "open"—this key, if it exists, will be the default (i.e. when a user double clicks on a file of your type, the open command will be performed). Instead, you can set the default value for the "shell" key equal to the verb you'd like to perform by default. You can optionally set the default value for each verb key to the text that you would like to appear in the context menu when a user right-clicks on a file of your type. An ampersand (&) can be used within this text to designate that the following character will be underlined, which means that the user can press the key corresponding to that character to select that command from the context menu. For instance, for the "open" key, you could put "Open with &Joe Blow's app" as the default value. That text then, with an underlined J, will appear in the context menu for files of that type, and a user can press the letter J to start Joe Blow's app.

无论您是否选择使用该DefaultIcon密钥,您现在都需要在您的JoeBlowFile密钥下创建一个名为“shell”的子项。在下面shell键,您将为您希望用户能够从上下文菜单(右键单击菜单)对您的文件类型执行的每个命令创建单独的键。这些项目被称为“动词”。为保持一致性,其中之一应该是“open”——这个键,如果它存在,将是默认的(即当用户双击你类型的文件时,将执行打开命令)。相反,您可以将“shell”键的默认值设置为您希望默认执行的动词。您可以选择将每个动词键的默认值设置为您希望在用户右键单击您类型的文件时出现在上下文菜单中的文本。可以在此文本中使用与号 (&) 来指定以下字符将带有下划线,这意味着用户可以按下与该字符对应的键来从上下文菜单中选择该命令。例如,对于“打开”键,您可以将“使用 &Joe Blow 的应用程序打开”作为默认值。然后,带有下划线 J 的文本将出现在该类型文件的上下文菜单中,用户可以按字母 J 启动 Joe Blow 的应用程序。

The only thing you haveto do, though, with the "open" (and subsequent) keys, is create another key as a subkey of that one called "command". The default value for the command key must be set to a string representing just that—the command required to perform that action. For instance, the default string in the command key under the "open" key might be ""C:\Program Files\JoeBlow\JoeBlow.exe" "%1"". Note the quotation marks around the path\filename for your app and around the %1. They are only neccessary around the path\filename of your app if there are any spaces in the path or filename, but they are absolutely a requirement around the %1for 32-bit apps. The %1is just like %1in old MS-DOS batch (.bat) files. %1is replaced with the first parameter on the command line, which in this case becomes the file name of the file your app is supposed to open. Because you do not know in advance if the path or filename containing the file you're supposed to open will contain spaces, you must put the quotes around %1.

你唯一必须做的,不过,与“开放”(以及随后的)键,是创建另一个关键作为一个所谓的“命令”的一个子项。命令键的默认值必须设置为一个字符串,代表执行该操作所需的命令。例如,“打开”键下的命令键中的默认字符串可能是“C:\Program Files\JoeBlow\JoeBlow.exe”“%1””。请注意您的应用程序的路径\文件名和%1. 如果路径或文件名中有任何空格,则它们仅在您的应用程序的路径\文件名周围是必需的,但%1对于 32 位应用程序,它们绝对是必需的。这%1就像%1在旧的 MS-DOS 批处理 (.bat) 文件中一样。 %1替换为命令行上的第一个参数,在这种情况下,它成为您的应用程序应该打开的文件的文件名。因为您事先不知道包含您应该打开的文件的路径或文件名是否包含空格,所以您必须将引号放在%1.

Other required parameters for your app should also be included. For instance, the default value in the "command" key, under the "print" key might be ""C:\Program Files\JoeBlow\JoeBlow.exe" "%1" /print", or ""C:\Program Files\JoeBlow\JoeBlow.exe" /print "%1"". It's up to you how you want to process command line parameters in your app.

还应包括应用程序的其他必需参数。例如,“命令”键中的默认值,在“打印”键下可能是““C:\Program Files\JoeBlow\JoeBlow.exe”“%1”/print”,或“”C:\Program Files\JoeBlow\JoeBlow.exe" /print "%1""。如何处理应用程序中的命令行参数取决于您。

A note on replaceable parameters like "%1", mentioned above. Apparently, the "%1" parameter /may/ be replaced with the shortfilename to be opened. This isn't always the case, and I haven't figured out what criteria Windows uses to determine which it will pass—short or long. It may be that if the executable path listed in the registry is a long filename, Windows will replace %1 with the long filename to start, but if the executable path is a short filename, or can be interpreted as one, Windows will replace %1with the short filename. If you want to be sure that you always get the longfilename, use "%L" instead. You can use an uppercase L (as I've done) or a lowercase one, but I prefer to use uppercase because lowercase "l" looks way too much like the number "1".

What's more, if your program knows how to deal with Shell Item IDs, you can get thatinstead of the long filename with the "%i" parameter. Again, upper- or lowercase "i" are equally suitable, but I find uppercase "I" harder to distiguish from lowercase "l" and the number "1". If you don't know what a Shell Item ID is, it's okay. You'll probably never need to use one.

上面提到的关于可替换参数(如“%1”)的注释。显然,“%1”参数 /may/ 被替换为要打开的文件名。情况并非总是如此,我还没有弄清楚 Windows 使用什么标准来确定它会通过什么标准——短期还是长期。可能是如果注册表中列出的可执行路径是长文件名,Windows 会用长文件名替换 %1 来启动,但如果可执行路径是短文件名,或者可以解释为一个,Windows 会替换%1为短文件名。如果您想确保始终获得文件名,请改用“%L”。您可以使用大写的 L(就像我所做的那样)或小写的,但我更喜欢使用大写,因为小写的“l”

更重要的是,如果您的程序知道如何处理 Shell Item ID,您可以使用“%i”参数获取而不是长文件名。同样,大写或小写的“i”同样适用,但我发现大写的“I”更难与小写的“l”和数字“1”区分开来。如果您不知道 Shell Item ID 是什么,也没关系。你可能永远不需要使用一个。

You're finally done with the JoeBlowFilekey. The rest is relatively simple. You simply need to create (if it doesn't already exist) another subkey under HKEY_CLASSES_ROOT, and name it the same as the extension of your document type. To use the txtfile example, the name would be ".txt" (with the dot, but without the quotes). Yours (Joe Blow's) might be ".jbf" (for Joe Blow File). The default value for this key must now be set to the name of the first key your created, which in the example we've using is "JoeBlowFile".

你终于完成了JoeBlowFile钥匙。剩下的就比较简单了。您只需在 HKEY_CLASSES_ROOT 下创建(如果尚不存在)另一个子项,并将其命名为与您的文档类型的扩展名相同。要使用 txtfile 示例,名称将是“.txt”(带点,但不带引号)。您的(Joe Blow 的)可能是“.jbf”(对于 Joe Blow 文件)。此键的默认值现在必须设置为您创建的第一个键的名称,在我们使用的示例中为“JoeBlowFile”。

That's it. You're done in the registry. Do remember that you'll have to make sure your app proccesses the command line in a manner consistent with the commands you set under the "shell" key. Window's won't open that file for you automatically when your app starts... you have to do it.

就是这样。您已在注册表中完成。请记住,您必须确保您的应用程序以与您在“shell”键下设置的命令一致的方式处理命令行。当您的应用程序启动时,Window 不会自动为您打开该文件……您必须这样做。

Graphically, it looks like this:

从图形上看,它看起来像这样:

HKEY_CLASSES_ROOT 
| +--.jbf = JoeBlowFile 
| 
+--JoeBlowFile = Joe Blow Data 
     | 
     +--DefaultIcon = C:\Program Files\JoeBlow\JoeBlow.exe, 2 
     | 
     +--Shell 
          | 
          +--open = Open with &Joe Blow's App 
          |     | 
          |     +--command = "C:\Program Files\JoeBlow\JoeBlow.exe" "%1" 
          | 
          +--print 
               | 
               +--command = "C:\Program Files\JoeBlow\JoeBlow.exe" "%1" /print

If you don't already know how to modify the registry, look in MSDN for all the functions beginning with "Reg", including RegOpenKeyEx, RegCreateKeyEx, and RegSetValueEx. You can also do it the wimpy way by creating a ".reg" file and simply use ShellExecuteEx() to call "regedit.exe /s" on it. (The /skeeps Regedit from popping up a message box asking "Are you sure you want to add the information in [name of file.reg] to the registry?") The format of the a REG file is simple and straight forward. Here is an example REG file to add the "JoeBlow" example from above:

如果您还不知道如何修改注册表,请在 MSDN 中查找所有以“Reg”开头的函数,包括 RegOpenKeyEx、RegCreateKeyEx 和 RegSetValueEx。您也可以通过创建“.reg”文件并简单地使用 ShellExecuteEx() 在其上调用“regedit.exe /s”来实现这一点。(/s防止 Regedit 弹出消息框询问“您确定要将 [file.reg 名称] 中的信息添加到注册表吗?”) REG 文件的格式简单明了。这是一个示例 REG 文件,用于添加上面的“JoeBlow”示例:

REGEDIT4 

[HKEY_CLASSES_ROOT\.jbf]
@="JoeBlowFile"

[HKEY_CLASSES_ROOT\JoeBlowFile]
@="Joe Blow Data"

[HKEY_CLASSES_ROOT\JoeBlowFile\DefaultIcon]
@="C:\Program Files\JoeBlow\JoeBlow.exe, 2"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell]

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\open]
@="Open with &Joe Blow's app"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\open\command]
@="\"C:\Program Files\JoeBlow\JoeBlow.exe\" \"%1\""

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\print]
@="&Print"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\print\command]
@="\"C:\Program Files\JoeBlow\JoeBlow.exe \"%1\" /print" 

Make sure that you include "REGEDIT4" as the first line of the file, or it won't work. Also make sure to press enter on the last line, or that line won't be read in. Altogether, adding your program to the registry this way is not as convenient as it sounds, because you'll have to modify your REG file if your app is installed anywhere exceptto C:\Program Files\JoeBlow.

确保将“REGEDIT4”作为文件的第一行包含在内,否则它将不起作用。还要确保在最后一行按回车键,否则该行将不会被读入。总而言之,以这种方式将程序添加到注册表并不像听起来那么方便,因为如果出现以下情况,您将不得不修改 REG 文件您的应用程序安装在C:\Program Files\JoeBlow之外的任何地方。

The above instructions were aimed at a user programming directly to the Win32 API using C or C++. For C# on .NET, it's rather a bit easier. See the Registry class, or you can even do much of it graphically using a deployment project in VS.NET.

上述说明针对的是使用 C 或 C++ 直接对 Win32 API 进行编程的用户。对于 .NET 上的 C#,它更容易一些。查看 Registry 类,或者您甚至可以使用 VS.NET 中的部署项目以图形方式完成大部分工作。



To add native-accessible resources to a .NET assembly, you will need a resource script. A resource script is a plain text file, like a code file. In fact, it is code; declarative code that is compiled by the resource compiler, rc.exe. A sample resource script follows:

要将本机可访问的资源添加到 .NET 程序集,您将需要一个资源脚本。资源脚本是一个纯文本文件,就像一个代码文件。其实就是代码;由资源编译器 rc.exe 编译的声明性代码。示例资源脚本如下:

#include <windows.h>

#define IDI_APP    100
#define IDI_FILE   200
#define ID_VERSION   1

IDI_APP  ICON "Resources\Application.ico"
IDI_FILE ICON "Resources\JowBlowFile.ico"

ID_VERSION VERSIONINFO
    FILEVERSION 1, 0, 19, 186     // change this to your version
    PRODUCTVERSION 1, 0, 19, 186  // change this to your version
    FILEOS VOS__WINDOWS32
    FILETYPE VFT_APP {
        BLOCK "StringFileInfo" {
            BLOCK "040904B0" { // 0x409 = U.S. English, 0x04B0 = dec 1200 = Unicode
                VALUE "CompanyName",      "Joe Blow, Inc.##代码##"
                VALUE "FileDescription",  "Joe Blow's App##代码##"
                VALUE "FileVersion",      "1.0.19.186##代码##" // change this to your version
                VALUE "InternalName",     "JoeBlow##代码##"
                VALUE "LegalCopyright",   "Copyright ? 2008-2009 Joe Blow Incorporated##代码##"
                VALUE "OriginalFilename", "JoeBlow.exe##代码##"
                VALUE "ProductName",      "Joe Blow##代码##"
                VALUE "ProductVersion",   "1.0.19.189##代码##" // change this to your version
            }
        }
        BLOCK "VarFileInfo" {
            VALUE "Translation", 0x409 /*U.S. English*/, 1200 /*Unicode*/
        }
    }

The biggest drawback to doing this is that you have to add the version information manually to your resource script (unless you want to just forgo the version information altogether). In my applications, I add a custom build step that runs a program I wrote that updates the version information directly in the executable so that I don't have to manually update the version number in the resource script, but that program is not suitable for public release and is otherwise beyond the scope of this post.

这样做的最大缺点是您必须手动将版本信息添加到资源脚本中(除非您想完全放弃版本信息)。在我的应用程序中,我添加了一个自定义构建步骤,该步骤运行我编写的程序,该程序直接在可执行文件中更新版本信息,这样我就不必手动更新资源脚本中的版本号,但该程序不适合公开发布,否则超出了本文的范围。

Now you need to invoke the resource compiler to build this resource script into a binary resource file. Save this script as JoeBlow.rc, then start a Visual Studio Command Prompt. It's under Tools in the Visual Studio start menu folder. If you don't have Visual Studio installed, I believe you get rc.exe as part of the SDK. Microsoft also seems to be offering the latest version here.

现在您需要调用资源编译器将这个资源脚本构建成一个二进制资源文件。将此脚本另存为 JoeBlow.rc,然后启动 Visual Studio 命令提示符。它位于 Visual Studio 开始菜单文件夹中的工具下。如果您没有安装 Visual Studio,我相信您将 rc.exe 作为 SDK 的一部分。微软似乎也在此处提供最新版本。

Once at a VS cmd prompt (or otherwise have rc.exe in your path), just type:

一旦在 VS cmd 提示符下(或者在你的路径中有 rc.exe),只需输入:

rc JoeBlow.rc

rc JoeBlow.rc

Simple as that. The above resource script should compile without errors, given that the icons I've include exist. This will create a new file in the same directory called JoeBlow.res. Now, assuming you areusing Visual Studio, all you have to do is edit the project properties to include this resource file.

就那么简单。鉴于我包含的图标存在,上面的资源脚本应该可以无误地编译。这将在名为 JoeBlow.res 的同一目录中创建一个新文件。现在,假设你正在使用Visual Studio,所有你需要做的就是编辑的项目属性包含此资源文件。

These directions are for Visual Studio 2005 or 2008. I don't remember how to do this, or even if you can, in older versions, and I haven't tried out 2010, yet, but it's probably similar. Right-click on the project in Solution Explorer and select Properties (or select Properties from the Project menu on the main menu bar). On the Application tab, which is the first tab you should see, at the bottom is a Resources group box. Here, you have two options: "Icon and manifest", or "Resource File". Select the latter option. This will enable the text box where you can type (or browse to) your new resource file, JoeBlow.res.

这些说明适用于 Visual Studio 2005 或 2008。我不记得如何在旧版本中执行此操作,或者即使您可以执行此操作,而且我还没有尝试过 2010,但它可能是相似的。在解决方案资源管理器中右键单击项目并选择属性(或从主菜单栏的项目菜单中选择属性)。在应用程序选项卡上,这是您应该看到的第一个选项卡,底部是资源组框。在这里,您有两个选项:“图标和清单”或“资源文件”。选择后一个选项。这将启用文本框,您可以在其中键入(或浏览到)新的资源文件 JoeBlow.res。

Lastly, just build your project, and presto, you have embedded icons in native PE format accessible to the shell when browsing files associated to your app. Now if you set the value of HKEY_CLASSES_ROOT\JoeBlowFile\DefaultIconto either C:\Program Files\JoeBlow\JoeBlow.exe,1(using the zero-based index number), or C:\Program Files\JoeBlow\JoeBlow.exe,-200(using the negative of the resource identifier, #defined above as IDI_FILE), your icon will show up in Explorer for .jbf files.

最后,只需构建您的项目,并且在浏览与您的应用程序相关联的文件时,您已经嵌入了原生 PE 格式的图标,shell 可以访问这些图标。现在,如果您将 的值设置HKEY_CLASSES_ROOT\JoeBlowFile\DefaultIconC:\Program Files\JoeBlow\JoeBlow.exe,1(使用从零开始的索引号)或C:\Program Files\JoeBlow\JoeBlow.exe,-200(使用资源标识符的负数,#defined 以上为IDI_FILE),您的图标将显示在资源管理器中,用于 .jbf 文件。

To get your new icons to show up immediately upon installation, you may need to refresh the shell's icon cache. I've found instructions on how to do that here. The basic gist is to change the shell icon size (at HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics) from its current value to a different one and back again, broadcasting a WM_SETTINGCHANGEmessage after each change.

要在安装后立即显示新图标,您可能需要刷新外壳的图标缓存。我在此处找到了有关如何执行操作的说明。基本要点是将外壳图标大小 (at HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics) 从其当前值更改为不同的值,然后再返回,WM_SETTINGCHANGE在每次更改后广播一条消息。

Good luck. Let me know if you need anything else.

祝你好运。需要帮助请叫我。

回答by Jeremy McGee

Hereis a discussion about how to use command-line utilities assocand ftypeto do this. You could invoke the command shell during a program deployment (or when the application is run) to do this.

是有关如何使用命令行实用程序assocftype执行此操作的讨论。您可以在程序部署期间(或在应用程序运行时)调用命令外壳来执行此操作。