简体   繁体   中英

C# - Add a "Network Location", not a mapped drive

I can successfully create a mapped drive (drive with a corresponding letter) using C#. What I haven't been able to reproduce is adding a "Network location" in C#. Everything I've looked up points back to some form or variation of "net use.." - When I use the built-in wizard the newly created "Network location" is stored in %APPDATA%\\Roaming\\Microsoft\\Windows\\Network Shortcuts - The properties point to a unique url and as best I can tell it is a special type of folder shortcut.

Does anyone know if it is possible to add a "Network Location" in C#, and not a mapped drive?

Any help is appreciated. Thanks.

添加网络位置

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();

        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM