WiX v3.7-vbScript自定义操作BrowseForFolder()不返回单个文件名
我找到了一个用于打开文件浏览器的vbscript示例,我在WiX中的自定义操作中使用了该示例。但是,我使用的vbscript函数名为WiX v3.7-vbScript自定义操作BrowseForFolder()不返回单个文件名,vbscript,wix,windows-installer,custom-action,wix3.7,Vbscript,Wix,Windows Installer,Custom Action,Wix3.7,我找到了一个用于打开文件浏览器的vbscript示例,我在WiX中的自定义操作中使用了该示例。但是,我使用的vbscript函数名为BrowseForFolder()(不是browseforfile),它似乎只在选择目录时返回值,而在选择单个文件时不返回值。以下是自定义操作: <CustomAction Id="File" Script="vbscript" Execute="immediate" Return="ignore"> <![CDATA[ Dim she
BrowseForFolder()
(不是browseforfile),它似乎只在选择目录时返回值,而在选择单个文件时不返回值。以下是自定义操作:
<CustomAction Id="File" Script="vbscript" Execute="immediate" Return="ignore">
<![CDATA[
Dim shell
Set shell = CreateObject("Shell.Application")
Dim file
Set file = shell.BrowseForFolder(0, "Choose a file:", &H4000)
Session.Property("FileName") = file.self.Path
]]>
</CustomAction>
使用这种方法,我实际上可以在对话框中看到单个文件,这是wix内置目录浏览器的一个改进
现在我只需要能够检索单个文件名,而不仅仅是文件夹名。任何帮助或建议都将不胜感激 我找到了这个代码
并对其进行了一些修改,以便更好地理解
WScript.Echo GetOpenFileName("C:\", "")
'
' Description: VBScript/VBS open file dialog
' Compatible with most Windows platforms
' Author: wangye <pcn88 at hotmail dot com>
' Website: http://wangye.org
'
' dir is the initial directory; if no directory is
' specified "Desktop" is used.
' filter is the file type filter; format "File type description|*.ext"
'
'
Public Function GetOpenFileName(dir, filter)
Const msoFileDialogFilePicker = 3
If VarType(dir) <> vbString Or dir="" Then
dir = CreateObject( "WScript.Shell" ).SpecialFolders( "Desktop" )
End If
If VarType(filter) <> vbString Or filter="" Then
filter = "All files|*.*"
End If
' try to choose the way to open the dialog box. Array: TryObjectNames
Dim i,j, objDialog, TryObjectNames
TryObjectNames = Array( _
"UserAccounts.CommonDialog", _
"MSComDlg.CommonDialog", _
"MSComDlg.CommonDialog.1", _
"Word.Application", _
"SAFRCFileDlg.FileOpen", _
"InternetExplorer.Application" _
)
On Error Resume Next
Err.Clear
For i=0 To UBound(TryObjectNames)
Set objDialog = WSH.CreateObject(TryObjectNames(i))
If Err.Number <> 0 Then
Err.Clear
Else
Exit For
End If
Next
' Select the way to dealing the object dialog
Select Case i
Case 0,1,2
' 0. UserAccounts.CommonDialog XP Only.
' 1.2. MSComDlg.CommonDialog MSCOMDLG32.OCX must registered.
If i=0 Then
objDialog.InitialDir = dir
Else
objDialog.InitDir = dir
End If
objDialog.Filter = filter
If objDialog.ShowOpen Then
GetOpenFileName = objDialog.FileName
End If
Case 3
' 3. Word.Application Microsoft Office must installed.
objDialog.Visible = False
Dim objOpenDialog, filtersInArray
filtersInArray = Split(filter, "|")
Set objOpenDialog = _
objDialog.Application.FileDialog( _
msoFileDialogFilePicker)
With objOpenDialog
.Title = "Open File(s):"
.AllowMultiSelect = False
.InitialFileName = dir
.Filters.Clear
For j=0 To UBound(filtersInArray) Step 2
.Filters.Add filtersInArray(j), _
filtersInArray(j+1), 1
Next
If .Show And .SelectedItems.Count>0 Then
GetOpenFileName = .SelectedItems(1)
End If
End With
objDialog.Visible = True
objDialog.Quit
Set objOpenDialog = Nothing
Case 4
' 4. SAFRCFileDlg.FileOpen xp 2003 only
' See http://www.robvanderwoude.com/vbstech_ui_fileopen.php
If objDialog.OpenFileOpenDlg Then
GetOpenFileName = objDialog.FileName
End If
Case 5
Dim IEVersion,IEMajorVersion, hasCompleted
hasCompleted = False
Dim shell
Set shell = CreateObject("WScript.Shell")
' ????IE??
IEVersion = shell.RegRead( _
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Version")
If InStr(IEVersion,".") > 0 Then
' ??????
IEMajorVersion = CInt(Left(IEVersion, InStr(IEVersion,".")-1))
If IEMajorVersion > 7 Then
' ???????7,?????IE7,???MSHTA??
' Bypasses c:\fakepath\file.txt problem
' http://pastebin.com/txVgnLBV
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
Dim tempName : tempName = fso.GetTempName()
Dim tempFile : Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")
Dim tempBaseName
tempBaseName = tempFolder & "\" & tempName
tempFile.Write _
"<html>" & _
" <head>" & _
" <title>Browse</title>" & _
" </head>" & _
" <body>" & _
" <input type='file' id='f'>" & _
" <script type='text/javascript'>" & _
" var f = document.getElementById('f');" & _
" f.click();" & _
" var fso = new ActiveXObject('Scripting.FileSystemObject');" & _
" var file = fso.OpenTextFile('" & _
Replace(tempBaseName,"\", "\\") & ".txt" & "', 2, true);" & _
" file.Write(f.value);" & _
" file.Close();" & _
" window.close();" & _
" </script>" & _
" </body>" & _
"</html>"
tempFile.Close
Set tempFile = Nothing
Set tempFolder = Nothing
shell.Run tempBaseName & ".hta", 1, True
Set tempFile = fso.OpenTextFile(tempBaseName & ".txt", 1)
GetOpenFileName = tempFile.ReadLine
tempFile.Close
fso.DeleteFile tempBaseName & ".hta"
fso.DeleteFile tempBaseName & ".txt"
Set tempFile = Nothing
Set fso = Nothing
hasCompleted = True ' ??????
End If
End If
If Not hasCompleted Then
' 5. InternetExplorer.Application IE must installed
objDialog.Navigate "about:blank"
Dim objBody, objFileDialog
Set objBody = _
objDialog.document.getElementsByTagName("body")(0)
objBody.innerHTML = "<input type='file' id='fileDialog'>"
while objDialog.Busy Or objDialog.ReadyState <> 4
WScript.sleep 10
Wend
Set objFileDialog = objDialog.document.all.fileDialog
objFileDialog.click
GetOpenFileName = objFileDialog.value
End If
objDialog.Quit
Set objFileDialog = Nothing
Set objBody = Nothing
Set shell = Nothing
Case Else
MsgBox("No file dialog component found", MsgBoxStyle.Exclamation, "Error")
End Select
Set objDialog = Nothing
End Function
WScript.Echo GetOpenFileName(“C:\”,“”)
'
'说明:VBScript/VBS打开文件对话框
'与大多数Windows平台兼容
"作者:王野
"网址:http://wangye.org
'
'dir是初始目录;如果没有目录
'使用指定的“桌面”。
'过滤器是文件类型过滤器;格式“文件类型说明|*.ext”
'
'
公共函数GetOpenFileName(dir,filter)
常量msoFileDialogFilePicker=3
如果VarType(dir)vbString或dir=“”,则
dir=CreateObject(“WScript.Shell”).SpecialFolders(“桌面”)
如果结束
如果VarType(filter)vbString或filter=“”,则
filter=“所有文件|*.*”
如果结束
'尝试选择打开对话框的方式。数组:TryObjectNames
Dim i、j、objDialog、TRYOBJECTNAME
TryObjectNames=数组(_
“UserAccounts.CommonDialog”_
“MSComDlg.CommonDialog”_
“MSComDlg.CommonDialog.1”_
“Word.Application”_
“SAFRCFileDlg.FileOpen”_
“InternetExplorer.Application”_
)
出错时继续下一步
呃,明白了
对于i=0到UBound(TryObjectNames)
Set objDialog=WSH.CreateObject(TryObjectNames(i))
如果错误号为0,则
呃,明白了
其他的
退出
如果结束
下一个
'选择处理对象对话框的方式
选择案例一
案例0,1,2
' 0. 仅限UserAccounts.CommonDialog XP。
' 1.2. MSComDlg.CommonDialog MSCOMDLG32.OCX必须注册。
如果i=0,那么
objDialog.InitialDir=dir
其他的
objDialog.InitDir=dir
如果结束
objDialog.Filter=过滤器
如果objDialog.ShowOpen,则
GetOpenFileName=objDialog.FileName
如果结束
案例3
' 3. 必须安装Word应用程序Microsoft Office。
objDialog.Visible=False
暗淡的对象显示日志、过滤器显示
filtersInArray=Split(过滤器“|”)
Set objOpenDialog=_
objDialog.Application.FileDialog(_
msoFileDialogFilePicker)
用objOpenDialog
.Title=“打开文件:”
.AllowMultiSelect=False
.InitialFileName=dir
.过滤器
对于j=0到UBound(filtersInArray)步骤2
.Filters.Add filtersInArray(j)_
过滤器阵列(j+1),1
下一个
如果.Show和.SelectedItems.Count>0,则
GetOpenFileName=.SelectedItems(1)
如果结束
以
objDialog.Visible=True
objDialog.Quit
Set objOpenDialog=无
案例4
' 4. SAFRCFileDlg.FileOpen xp 2003仅限
”“看到了吗http://www.robvanderwoude.com/vbstech_ui_fileopen.php
如果是objDialog.OpenFileOpenDlg,则
GetOpenFileName=objDialog.FileName
如果结束
案例5
Dim IEVersion,IEMajorVersion,已完成
hasCompleted=False
暗壳
Set shell=CreateObject(“WScript.shell”)
“即??
IEVersion=外壳。重新研磨(_
“HKEY\本地\计算机\软件\微软\ Internet Explorer\版本”)
如果仪表(IEVersion,“.”)大于0,则
' ??????
IEMajorVersion=CInt(左(IEVersion,仪表(IEVersion,“.”-1))
如果IEMajorVersion>7,则
“7、IE7、MSHTA”??
'绕过c:\fakepath\file.txt问题
' http://pastebin.com/txVgnLBV
模糊fso
设置fso=CreateObject(“Scripting.FileSystemObject”)
Dim tempFolder:Set tempFolder=fso.GetSpecialFolder(2)
Dim tempName:tempName=fso.GetTempName()
Dim tempFile:Set tempFile=tempFolder.CreateTextFile(tempName&“.hta”)
Dim tempBaseName
tempBaseName=tempFolder&“\”&tempName
tempFile.Write_
"" & _
" " & _
"浏览"及_
" " & _
" " & _
" " & _
" " & _
“var f=document.getElementById('f');”&_
“f.单击();”和_
“var fso=new ActiveXObject('Scripting.FileSystemObject');”&_
“var file=fso.OpenTextFile('”&_
替换(tempBaseName,“\”,“\”&“.txt”&“,,2,true);“&”_
“file.Write(f.value);”和_
“file.Close();”和_
“window.close();”和_
" " & _
" " & _
""
tempFile.Close
设置tempFile=Nothing
设置tempFolder=Nothing
运行tempBaseName&“.hta”,1,True
设置tempFile=fso.OpenTextFile(tempBaseName&“.txt”,1)
GetOpenFileName=tempFile.ReadLine
tempFile.Close
fso.delete文件tempBaseName&“.hta”
fso.delete文件tempBaseName&“.txt”
设置tempFile=Nothing
设置fso=无
hasCompleted=True'??????
如果结束
如果结束
如果没有完成,那么
' 5. InternetExplorer。必须安装应用程序IE
objDialog.导航“关于:空白”
Dim objBody,objFileDialog
设置对象=_
objDialog.document.getElementsByTagName(“正文”)(0)
objBody.innerHTML=“”
objDialog.Busy或objDialog.ReadyState 4时
WScript.sleep 10
温