vb.net 获取当前目录并在 vbscript 中运行文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9530646/
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
Get current directory and run a File in vbscript?
提问by deception1
I am trying to see if IIS is installed and display a message and a download exe to INstall IIS if IIS isn't installed.However i am having a hard time running a file without specifying the full path in the vb-script.The path will be dynamic and it impossible to specify any other directory than "%cd%
我正在尝试查看 IIS 是否已安装,如果未安装 IIS,则显示一条消息和一个下载 exe 到 INstall IIS。但是我很难在不指定 vb-script 中的完整路径的情况下运行文件。路径将是动态的,不可能指定除“%cd%”以外的任何其他目录
My code:
我的代码:
If WScript.Arguments.length =0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
Dim intCounter, strSubkey
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\" _
& strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft"
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
intCounter=0
For Each subkey In arrSubKeys
If subkey="InetStp" Then
intCounter=1 or strSubkey=subkey
End If
Next
currentDirectory = left(WScript.ScriptFullName, Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
if intCounter=0 then
Set WSHShell = CreateObject("Wscript.Shell")
WSHShell.Run ("\currentDirectory\noiisinstalled.exe")
Elseif intCounter=1 then
Wscript.Echo "IIS is Already installed - " & strSubkey
End If
End if
My problem is running the no iisinstalled.exe file.Whatever I'm trying the script cannot find the file.
我的问题是运行没有 iisinstalled.exe 文件。无论我尝试什么脚本都找不到该文件。
回答by
You can get the current directory using the Scripting.FileSystemObject
. ie
您可以使用Scripting.FileSystemObject
. IE
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
' directory in which this script is currently running
CurrentDirectory = fso.GetAbsolutePathName(".")
to use this to build a new path, you can use the BuildPath()
function
要使用它来构建新路径,您可以使用该BuildPath()
功能
NewPath = fso.BuildPath(CurrentDirectory, "noiisinstalled.exe")
回答by Nathan Rice
Set WSHShell = CreateObject("Wscript.Shell")
sCurrentDirectory = WSHShell.CurrentDirectory & "\"