Ami*_*and 8 .net vb.net registry system.diagnostics file
我们已经知道它是如何System.Diagnostics.Process.Start("C:\filename.png")
工作的,但如果文件名不以扩展名结尾怎么办?
如何使用与扩展名相关联的默认程序运行没有扩展名的文件.png
名?
就像是:
openFile("C:\filename","PNG")
Run Code Online (Sandbox Code Playgroud)
Jim*_*imi 33
您当然可以将FileName和Extension一起打包,并希望获得最佳效果.
或者测试文件扩展名是否具有预定义的Opener,即Registry中的一个条目,它将Extension与可能具有该文件格式的应用程序相关联.
系统提供了一个API - AssocQueryString - 提供了这些信息(当然,您也可以手动查找注册表,但事情可能会发生变化,API函数已经知道它).它对其他一些任务也很有用.
请参阅声明部分以查找VB.Net函数调用和相关枚举器.
这个辅助方法只允许调用API函数而无需指定特殊标志.您只需要定义要检索的信息类型.
例如:
AssociationQuery.Executable
:
返回与指定扩展名关联的可执行文件的完整路径.
AssociationQuery.Command
:当文件扩展名与独立可执行文件没有直接关联但使用dll或系统小程序时,
返回Rundll32
Shell执行的命令.或者返回与Shell动词关联的命令(打开,打印,编辑,新建等).
AssociationQuery.FriendlyDocName
:
返回关联文件类型的友好名称.例如,.docx
扩展名将返回Microsoft Word文档(取决于本地语言).
AssociationQuery.FriendlyAppName
:
返回关联应用程序的友好名称.例如,.docx
扩展名将返回Word 2016(取决于本地语言).
有关这些开关的更多信息以及声明部分中其他可用开关的说明,请参阅下面的示例.
的辅助方法(称它是如下所述):
Public Function FileExtentionInfo(AssociationType As AssociationQuery, Extension As String) As String
If (Extension.Length <= 1) OrElse Not Extension.StartsWith(".") Then
Return String.Empty
Else
Dim flags As AssocFlags = AssocFlags.NoTruncate Or AssocFlags.RemapRunDll
Return ShellExtensionInfo(True, AssociationType, flags, Extension)
End If
End Function
Run Code Online (Sandbox Code Playgroud)
该工法:
Private Function ShellExtensionInfo(Verify As Boolean, AssociationType As AssociationQuery, flags As AssocFlags, Extension As String) As String
Dim pcchOut As UInteger = 0
flags = flags Or (If(Verify, AssocFlags.Verify, AssocFlags.IgnoreUnknown))
AssocQueryString(flags, AssociationType, Extension, Nothing, Nothing, pcchOut)
If pcchOut = 0 Then
Return String.Empty
Else
Dim pszOut As New StringBuilder(CType(pcchOut, Integer))
AssocQueryString(flags, AssociationType, Extension, Nothing, pszOut, pcchOut)
Return pszOut.ToString()
End If
End Function
Run Code Online (Sandbox Code Playgroud)
要获得结果,只需调用辅助函数,询问AssociationQuery
您想要的信息类型或关联().
例如,这个电话:
Dim Executable As String = FileExtentionInfo(AssociationQuery.Executable, ".docx")
Run Code Online (Sandbox Code Playgroud)
可以返回(取决于安装的软件和用户选择):
C:\ Program Files\Microsoft Office\Office16\WINWORD.EXE
如前所述,并非所有扩展都具有关联的可执行文件.某些文件扩展名可能会映射到由其启动的系统小程序/工具Rundll32.exe
.
在这种情况下,使用AssociationQuery.Executable
作为参数可能不会返回可行的结果.
例如,如果未安装图像编辑软件,则可能会返回查询可执行文件.jpg
或.png
扩展名:
C:\ Program Files(x86)\ Windows Photo Viewer\PhotoViewer.dll
在这种情况下,使用AssociationQuery.Command
参数来获取相关的命令行,然后可以将其传递给Process.Start()
.
因此,在相同的情况下,结果将是:
C:\ Windows\System32\rundll32.exe"C:\ Program Files(x86)\ Windows Photo Viewer\PhotoViewer.dll",ImageView_Fullscreen%1
where %1
表示要打开的文件的名称.
在将该参数分配给ProcessStartInfo.Arguments属性之前,必须将该参数替换为文件路径.
使用与可执行文件相关联的文件扩展名(.docx
此处为MS Word 的扩展名)和与DLL组件关联的文件扩展名(.png扩展名,通常与预定义的系统小程序关联,如果没有其他查看者/编辑器关联的示例)用户或程序安装).
如示例所示,文件名不需要指定扩展名:关联程序可以在任何情况下识别和解码文件内容(即扩展名不定义文件格式):
Dim FileName = "C:\Files\Docs\SomeDoc"
'Dim FileName = "C:\Files\Images\SomeImage"
Dim execProgram As String = FileExtentionInfo(AssociationQuery.Executable, "docx")
'Dim execProgram As String = FileExtentionInfo(AssociationQuery.Executable, "png")
Dim execExtension = Path.GetExtension(execProgram).ToLower()
Dim pInfo As ProcessStartInfo = New ProcessStartInfo()
If execExtension.Contains("exe") Then
pInfo.FileName = execProgram
pInfo.Arguments = FileName
Else
Dim rundllCmd As String = FileExtentionInfo(AssociationQuery.Command, "png")
Dim rundllParts = rundllCmd.Split({ChrW(32) + ChrW(34)}, StringSplitOptions.None)
pInfo.FileName = rundllParts(0)
pInfo.Arguments = ChrW(34) & rundllParts(1).Replace("%1", FileName)
End If
pInfo.UseShellExecute = False
Process.Start(pInfo)
Run Code Online (Sandbox Code Playgroud)
声明:
VB.Net DllImport
for AssocQueryString()
.请参阅MSDN AssocQueryString函数
<DllImport("Shlwapi.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Public Shared Function AssocQueryString(flags As AssocFlags, str As AssociationQuery,
pszAssoc As String, pszExtra As String, <Out> pszOut As System.Text.StringBuilder,
<[In]> <Out> ByRef pcchOut As UInteger) As UInteger
End Function
Run Code Online (Sandbox Code Playgroud)
及其枚举:参见MSDN ASSOCF枚举,ASSOCSTR枚举
<Flags> _
Public Enum AssocFlags
None = &H0 'No option selected
Init_NoRemapCLSID = &H1 'Instructs the interface methods not to map CLSID values to ProgID values.
Init_ByExeName = &H2 'THe pszAssoc parameter is an executable file name: the root key for search is set to the executable file's ProgID.
Open_ByExeName = &H2 'Alias for Init_ByExeName
Init_DefaultToStar = &H4 'If the value is not found under the root key, it should retrieve the comparable value from the * subkey.
Init_DefaultToFolder = &H8 'If the value is not found under the root key, it should retrieve the comparable value from the Folder subkey.
NoUserSettings = &H10 'Only the HKEY_CLASSES_ROOT should be searched, HKEY_CURRENT_USER should be ignored.
NoTruncate = &H20 'The return string should not be truncated. Return an error value and the required size for the complete string.
Verify = &H40 'Instructs to verify that data is accurate. This setting allows to read data from the user's hard disk for verification.
RemapRunDll = &H80 'Instructs to ignore Rundll.exe and return information about its target.
NoFixUps = &H100 'Instructs not to fix errors in the registry, such as the friendly name of a function not matching the one found in the .exe file.
IgnoreBaseClass = &H200 'Specifies that the BaseClass value should be ignored.
IgnoreUnknown = &H400 'Windows 7: "Unknown" ProgID should be ignored; instead, fail.
InitFixedProgid = &H800 'Windows 8: The supp. ProgID should be mapped using the sys defaults, rather than the current user defaults.
IsProtocol = &H1000 'Windows 8: The value is a protocol, should be mapped using the current user defaults.
InitForFile = &H2000 'Windows 8.1: The ProgID corresponds with a file extension based association. Use together with InitFixedProgid.
End Enum
Public Enum AssociationQuery
Command = 1, 'A command string associated with a Shell verb (=> Open, Edit, Print, New).
Executable, 'An executable from a Shell verb command string. Not all associations have executables.
FriendlyDocName, 'The friendly name of a document type. (Microsoft Office 2016 Document)
FriendlyAppName, 'The friendly name of an executable file. (Microsoft Office Word)
NoOpen, 'Ignore the information associated with the open subkey.
ShellNewValue, 'Look under the ShellNew subkey.
DDECommand, 'A template for DDE commands.
DDEIfExec, 'The DDE command to use to create a process.
DDEApplication, 'The application name in a DDE broadcast.
DDETopic, 'The topic name in a DDE broadcast.
Infotip, 'The InfoTip registry value. Returns an info tip for an item from which to create an info tip, such as when hovering the cursor over a file name.
Quicktip, 'Corresponds to the QuickTip registry value. Similar to InfoTip, but more suitable for network transport.
TileInfo, 'Corresponds to the TileInfo registry value. Similar to InfoTip, for a file type in a window that is in tile view.
ContentType, 'Describes a general type of MIME file association, such as image and bmp, to make general assumptions about a specific file type.
DefaultIcon, 'Returns the path to the default icon for this association. Positive numbers: index of the dll's resource table, Negative numbers: resource ID.
ShellExtension, 'If a Shell extension is associated, use this to get the CLSID of the Shell extension object, passing the string of the IID as the pwszExtra parameter.
DropTarget, 'For a verb invoked through COM and the IDropTarget interface, use this flag to get the IDropTarget object's CLSID. The verb is specified in the pwszExtra parameter.
DelegateExecute, 'For a verb invoked through COM and the IExecuteCommand interface, use this flag to get the IExecuteCommand object's CLSID. This CLSID is registered in the verb's command subkey as the DelegateExecute entry. The verb is specified in the pwszExtra parameter.
SupportedURIProtocols, 'Windows 8
ProgID, 'The ProgID provided by the app associated with the file type or URI scheme. This if configured by users in their default program settings.
AppID, 'The AppUserModelID of the app associated with the file type or URI scheme. This is configured by users in their default program settings.
AppPublisher, 'The publisher of the app associated with the file type or URI scheme. This is configured by users in their default program settings.
AppIconReference, 'The icon reference of the app associated with the file type or URI scheme. This is configured by users in their default program settings.
Max 'The maximum defined ASSOCSTR value, used for validation purposes.
End Enum
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1219 次 |
最近记录: |