C# 使用文件路径C在收藏夹中创建快捷方式#

C# 使用文件路径C在收藏夹中创建快捷方式#,c#,special-folders,C#,Special Folders,我有下面的代码。我似乎无法将文件夹添加到收藏夹中。如果我将其更改为specialfolders.desktop,它会在桌面上创建一个快捷方式 private void buttonAddFav_Click(object sender, EventArgs e) { string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; string targetPath

我有下面的代码。我似乎无法将文件夹添加到收藏夹中。如果我将其更改为specialfolders.desktop,它会在桌面上创建一个快捷方式

private void buttonAddFav_Click(object sender, EventArgs e)
    {
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        string targetPath = listFolderResults.SelectedItem.ToString();
        var wsh = new IWshShell_Class();
        IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
            Environment.GetFolderPath(Environment.SpecialFolder.Favorites) + "\\shorcut2.lnk") as IWshRuntimeLibrary.IWshShortcut;
        shortcut.TargetPath = targetPath;
        shortcut.Save();
    }

环境.SpecialFolder.Favorites
表示包含Internet Explorer收藏夹的文件夹,这些收藏夹位于
%USERPROFILE%\Favorites
文件夹中

没有表示您的Windows资源管理器收藏夹的
环境.SpecialFolder
值,这些收藏夹位于
%USERPROFILE%\Links
文件夹中

要检索
Links
文件夹的路径,您必须使用以下任一方法直接查询Shell中的
FOLDERID\u Links
路径:

  • 平沃克打电话来

  • COM互操作,然后调用


  • Environment.SpecialFolder.Favorites
    表示包含Internet Explorer收藏夹的文件夹,这些收藏夹位于
    %USERPROFILE%\Favorites
    文件夹中

    没有表示您的Windows资源管理器收藏夹的
    环境.SpecialFolder
    值,这些收藏夹位于
    %USERPROFILE%\Links
    文件夹中

    要检索
    Links
    文件夹的路径,您必须使用以下任一方法直接查询Shell中的
    FOLDERID\u Links
    路径:

  • 平沃克打电话来

  • COM互操作,然后调用


  • 下面的代码最终为我工作。下面的答案有助于确定我需要围绕%userprofile%\links进行构建,但是%userprofile%在保存时给了我错误。当我使用下面的方法时,它起了作用

    string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            string targetPath = listFolderResults.SelectedItem.ToString();
            string shortcutPath = string.Format(@"C:\Users\{0}\Links",Environment.UserName);
            MessageBox.Show(shortcutPath);
    
            var wsh = new IWshShell_Class();
            IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
                shortcutPath + string.Format(@"\{0}.lnk",textFavName.Text)) as IWshRuntimeLibrary.IWshShortcut;
            shortcut.TargetPath = targetPath;
            shortcut.Save();
    

    下面的代码最终为我工作。下面的答案有助于确定我需要围绕%userprofile%\links进行构建,但是%userprofile%在保存时给了我错误。当我使用下面的方法时,它起了作用

    string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            string targetPath = listFolderResults.SelectedItem.ToString();
            string shortcutPath = string.Format(@"C:\Users\{0}\Links",Environment.UserName);
            MessageBox.Show(shortcutPath);
    
            var wsh = new IWshShell_Class();
            IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
                shortcutPath + string.Format(@"\{0}.lnk",textFavName.Text)) as IWshRuntimeLibrary.IWshShortcut;
            shortcut.TargetPath = targetPath;
            shortcut.Save();
    

    “收藏夹”是什么意思?能否上载屏幕截图?windows资源管理器中的“收藏夹”部分。我刚刚添加了一张图片,您正在测试哪个操作系统?我正在使用Windows 7。您所说的“收藏夹”可能是重复的,您可以上传屏幕截图吗?Windows资源管理器中的“收藏夹”部分。我刚刚添加了一个图像,你在测试哪个操作系统?我正在使用Windows 7可能的副本,我对C#还是相当陌生的。你能给我举个例子说明怎么做吗?谢谢大家!@肖恩登:我对C#还是相当陌生的。你能给我举个例子说明怎么做吗?谢谢大家!@SeanDon检查此答案:此代码不安全,因为您假定链接文件夹的位置位于C:\Users\中。但此特殊文件夹位置可以更改。在Windows资源管理器中的“链接”文件夹上单击鼠标右键,然后单击“位置”选项卡,然后输入“链接”特殊文件夹的物理文件夹位置。因此,如果用户已将其链接文件夹移动到其他位置,例如不同分区中的文件夹,则您的代码将失败。非常好的提示!我们可以使用环境.GetFolderPath(Environment.SpecialFolder.UserProfile)+@“\Links”,而不是静态字符串。谢谢。此代码不安全,因为您假定链接文件夹的位置位于C:\Users\中。但此特殊文件夹位置可以更改。在Windows资源管理器中的“链接”文件夹上单击鼠标右键,然后单击“位置”选项卡,然后输入“链接”特殊文件夹的物理文件夹位置。因此,如果用户已将其链接文件夹移动到其他位置,例如不同分区中的文件夹,则您的代码将失败。非常好的提示!我们可以使用Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+@“\Links”,而不是静态字符串。谢谢