C# - 添加“网络位置”,而不是映射驱动器

Posted

技术标签:

【中文标题】C# - 添加“网络位置”,而不是映射驱动器【英文标题】:C# - Add a "Network Location", not a mapped drive 【发布时间】:2021-12-06 10:25:07 【问题描述】:

我可以使用 C# 成功创建映射驱动器(具有相应字母的驱动器)。我无法重现的是在 C# 中添加“网络位置”。我查找的所有内容都指向“网络使用..”的某种形式或变体 - 当我使用内置向导时,新创建的“网络位置”存储在 %APPDATA%\Roaming\Microsoft\Windows\Network快捷方式 - 属性指向一个唯一的 url,据我所知,它是一种特殊类型的文件夹快捷方式。

有谁知道是否可以在 C# 中添加“网络位置”而不是映射驱动器?

感谢任何帮助。谢谢。

【问题讨论】:

您可以将this powershell script 改编为C#。答案还包含一大堆网络上可供阅读的参考资料。 谢谢,这成功了。编辑:我没有将您的回复标记为答案的选项。知道为什么吗? 哈,因为它只是一个“也许这行得通?”建议。如果您可以在下面发布您的 C# 改编作为自我回答,这样将来的人们可以从中受益,那就太好了。 希望对您有所帮助。 【参考方案1】:
using System;

using IWshRuntimeLibrary;

using System.IO;

 

namespace CustomShortcuts



    class Program

    
    
        static string networkPath = @"\\servername\path";

        static string networkFolderName = "Name you want users to see";

 

        static void Main(string[] args)

        

            createDesktopini(networkFolderName, networkPath, 0);

        

 

        static private void createDesktopini(string name, string targetPath, int iconNumber)

        

            string networkshortcuts = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Network Shortcuts");

            var newFolder = Directory.CreateDirectory(networkshortcuts + @"\" + name);

            newFolder.Attributes |= FileAttributes.ReadOnly;

            string desktopiniContents = @"[.ShellClassInfo]" + Environment.NewLine +

                "CLSID2=0AFACED1-E828-11D1-9187-B532F1E9575D" + Environment.NewLine +

                "Flags=2";

            string shortcutlocation = networkshortcuts + @"\" + name;

            System.IO.File.WriteAllText(shortcutlocation + @"\Desktop.ini", desktopiniContents);

            string targetLNKPath = networkshortcuts + @"\" + name;

            createShortcut(targetLNKPath, targetPath, iconNumber);

        

 

        static private void createShortcut(string shortcutPath, string targetPath, int iconNumber)

        

            WshShell shell = new WshShell();

            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath + @"\target.lnk");

            shortcut.Description = targetPath;

            shortcut.TargetPath = targetPath;

            shortcut.Arguments = targetPath;

            shortcut.IconLocation = @"%SystemRoot%\system32\imageres.dll, " + iconNumber;

            shortcut.Save();

        

    


【讨论】:

以上是关于C# - 添加“网络位置”,而不是映射驱动器的主要内容,如果未能解决你的问题,请参考以下文章

win10怎么映射网络驱动器,怎么连接共享文件夹

网络驱动器和网络位置

在windows中,为啥通过smb协议过来的ext4硬盘网络驱动器映射后,显示的是ntfs格式呢

如何在windows10系统中映射网络驱动器

FTP如何远程登录啊?

win7无法将linux的共享文件夹映射网络驱动器