You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用VBS实现与PowerShell等效的创建此电脑网络位置功能

VBS实现创建此电脑显示的网络位置

问题原因

你现有VBS代码无法生效的核心原因是Windows网络位置的存储结构不是直接在NetHood目录下放快捷方式,而是要遵循固定的目录结构规则,和你提供的PowerShell逻辑完全对应:

  • 先在NetHood目录下创建和网络位置同名的文件夹
  • 文件夹内创建Desktop.ini配置文件,指定网络位置的CLSID标识
  • 文件夹内创建名为target.lnk的快捷方式指向目标路径
  • 最后给文件夹和Desktop.ini设置对应属性,系统才会识别为正规网络位置显示在此电脑中。

完整可运行VBS代码

Const NETHOOD = &H13&
Const READONLY = 1
Const HIDDEN = 2
Const SYSTEM = 4

' 创建所需对象
Set objShell = CreateObject("Shell.Application")
Set objWSHShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' 获取NetHood路径
Set objFolder = objShell.Namespace(NETHOOD)
strNetHood = objFolder.Self.Path

' 配置你的网络位置参数
strLocationName = "Public"
strTargetPath = "\\ads01\Public"

strLocationFolder = strNetHood & "\" & strLocationName

' 检查条件:目标路径存在、同名网络位置不存在
If objFSO.FolderExists(strTargetPath) And Not objFSO.FolderExists(strLocationFolder) Then
    ' 1. 创建同名文件夹
    objFSO.CreateFolder strLocationFolder
    
    ' 2. 写入Desktop.ini配置
    strDesktopIniPath = strLocationFolder & "\Desktop.ini"
    Set iniFile = objFSO.CreateTextFile(strDesktopIniPath, True)
    iniFile.WriteLine "[.ShellClassInfo]"
    iniFile.WriteLine "CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}"
    iniFile.WriteLine "Flags=2"
    iniFile.WriteLine "ConfirmFileOp=1"
    iniFile.Close
    
    ' 3. 创建target.lnk快捷方式
    strShortcutPath = strLocationFolder & "\target.lnk"
    Set objShortcut = objWSHShell.CreateShortcut(strShortcutPath)
    objShortcut.TargetPath = strTargetPath
    objShortcut.IconLocation = "%SystemRoot%\system32\SHELL32.DLL, 85"
    objShortcut.Description = strTargetPath
    objShortcut.WorkingDirectory = strTargetPath
    objShortcut.Save
    
    ' 4. 设置属性
    ' Desktop.ini设为系统+隐藏
    Set iniFileObj = objFSO.GetFile(strDesktopIniPath)
    iniFileObj.Attributes = SYSTEM Or HIDDEN
    ' 父文件夹设为只读
    Set folderObj = objFSO.GetFolder(strLocationFolder)
    folderObj.Attributes = folderObj.Attributes Or READONLY
Else
    MsgBox "错误:同名网络位置已存在或目标路径不可访问"
End If

效果说明

运行后刷新此电脑,即可看到和PowerShell生成效果完全一致的网络位置,自动固定在此电脑的设备和驱动器分组下。


内容的提问来源于stack exchange,提问作者TaMèreLaBallerine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 18:27:00