geting special folders with shell.application

为君一笑 提交于 2019-12-10 00:10:19

问题


I need in a script to return the path to the current user desktop. Now I know you can do it with WScript.

var WshShell = WScript.CreateObject("WScript.Shell");
         strDesktop = WshShell.SpecialFolders("Desktop");

But for my script this will not work as I cant use WScript. but I can use the shell.application object as below.

 dim objShell
        dim ssfWINDOWS
        dim objFolder

        ssfWINDOWS = 0
        set objShell = CreateObject("shell.application")
            set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
                if (not objFolder is nothing) then
                Set objFolderItem = objFolder.Self
                    g_objIE.Document.All("logdir").Value = objFolderItem.path
                end if
            set objFolder = nothing
        set objShell = nothing

what is the syntax so the rather than "BrowseForFolder" i can simple return the path of the current users desktop?

IE replace the line

set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)

with the equilivent of.

strDesktop = WshShell.SpecialFolders("Desktop");

Cheers

Aaron


回答1:


You need to use Shell.Namespace(...).Self.Path:

Const ssfDESKTOPDIRECTORY = &h10
Set oShell = CreateObject("Shell.Application")
strDesktop = oShell.NameSpace(ssfDESKTOPDIRECTORY).Self.Path

WScript.Echo strDesktop


But for my script this will not work as I cant use WScript.

Do you mean you can't use WScript.CreateObject(...) because WScript is undefined? If so, you can simply use CreateObject("WScript.Shell").SpecialFolders("Desktop") instead. See What is the difference between CreateObject and Wscript.CreateObject?.




回答2:


Try the namespace method:

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H10&)

Where &H10& is a special folder constant for the desktop. See technet for a list of all special folder constants.



来源:https://stackoverflow.com/questions/8051450/geting-special-folders-with-shell-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!