相关疑难解决方法(0)

创建文件快捷方式(.lnk)

我一直在寻找一种在C#中创建文件快捷方式的简单方法,但我只找到了那样做的外部dll.这实际上是相当令人惊讶的,没有内置的方法来做到这一点..

无论如何,我知道lnk文件只是具有特定命令和给定路径的文本文件.我想也许我可以创建一个文本文件(在代码中)将它的文本设置为正确的命令并将其扩展名更改为.lnk我尝试先手动执行此操作,但未能这样做.

有没有办法做这样的事情(或者可能是另一种简单的方法)来创建c#中某个路径的快捷方式?

为了清楚起见,通过快捷方式我的意思是一个.lnk文件,它导致文件 编辑:而文件我指的是我想要的任何文件,而不仅仅是我自己的应用程序的快捷方式


如果它不适合每个场景,我会编辑.

添加以下参考:

  1. Microsoft Shell控件和自动化
  2. Windows脚本宿主对象模型

添加此命名空间:

using Shell32;
using IWshRuntimeLibrary;
Run Code Online (Sandbox Code Playgroud)

接下来似乎工作:

var wsh = new IWshShell_Class();
IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut2.lnk") as IWshRuntimeLibrary.IWshShortcut;
shortcut.TargetPath = @"C:\Users\Zimin\Desktop\test folder";            
shortcut.Save();
Run Code Online (Sandbox Code Playgroud)

希望它能帮助其他人,感谢您的关注.

另外,如果有一种创建文件的方法,请编写正确的命令,然后将其更改为lnk文件,请告诉我.

c# automation shortcut lnk

20
推荐指数
1
解决办法
4万
查看次数

如何创建开始菜单快捷方式

我正在构建自定义安装程序.如何在开始菜单中创建可执行文件的快捷方式?这是我到目前为止所提出的:

    string pathToExe = @"C:\Program Files (x86)\TestApp\TestApp.exe";
    string commonStartMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
    string appStartMenuPath = Path.Combine(commonStartMenuPath, "Programs", "TestApp");
    // TODO: create shortcut in appStartMenuPath
Run Code Online (Sandbox Code Playgroud)

我的目标是Windows 7.

c# shortcut .net-4.0

12
推荐指数
1
解决办法
1万
查看次数

在c#中创建文件夹的快捷方式

总而言之,我需要使用C#创建文件夹的快捷方式.我一直在阅读使用IWshRuntimeLibrary.当我去尝试使用任何东西时,IWshRuntimeLibrary我会遇到各种各样的歧义错误System.IO.File.我假设这是因为有一个IWshRuntimeLibrary.File接口System.IO.File.我真正能找到的所有文章都是关于制作应用程序快捷方式的文章,而非文件夹.

抛开歧义错误:

  • 这是用于文件夹快捷方式的正确工具吗?
  • 如何使用此创建快捷方式
  • 如何指定快捷方式的放置位置

此外,当我尝试创建文件夹的快捷方式时,请C:\TEMP使用以下命令:

IWshShortcut shortcut;
wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(@"C:\TEMP");

shortcut.TargetPath = @"C:\Documents and Settings";
Run Code Online (Sandbox Code Playgroud)

我得到了COMException.根据我所读到的,这应该创建一个C驱动器临时文件夹的快捷方式,并将快捷方式放在文档和设置中.

c# winforms

8
推荐指数
1
解决办法
1万
查看次数

IShellLink :: SetIconLocation将我的图标路径转换为%Program Files%,这是错误的

有谁知道如何纠正这种行为?

当前,当我们的安装程序安装我们的应用程序时,它将获取IShellLink,然后使用快捷方式图标(在开始菜单和桌面中)所需的数据将其加载,然后使用IPersistFile :: Save编写快捷方式。

问题是通过IShellLink :: SetIconLocation为图标指定的路径使用%ProgramFiles%...(对于x64 ...)被转换为WRONG。

我注意到很多其他32位软件在x64下都无法通过此操作-但后来我假定他们自己在自己的.lnk创建代码中使用%ProgamFiles%作为文字元素。但是,似乎是IShellLink强迫该错误存在,并且我没有解决方法(或者说Shell中的链接属性编辑器对此问题负责,并且基础链接可以)。

一些Google搜索没有发现任何问题……是否有人遇到过这个问题,或者是否知道一篇文章/示例,说明如何强制x64 Windows不要对此进行处理?

澄清的例子:

hr = m_shell_link->SetIconLocation("C:\\Program Files (x86)\\Acme\\Prog.exe", 0);
Run Code Online (Sandbox Code Playgroud)

将产生一个带有正确图标的快捷方式,但是当您在快捷方式属性页中按“更改图标”时,将报告“ Windows找不到文件%ProgramFiles%\ Acme \ Prog.exe”。

c++ windows-shell

5
推荐指数
1
解决办法
2724
查看次数

C#ShortCut路径修改

我创建了一个程序,该程序使用某些库生成通过打开的文件对话框选择的特定EXE的快捷方式。我可以使用它,但是我希望程序在目标路径中添加一个参数,使其看起来像这样:("E:\Cod4\iw3mp.exe" +Seta Map mp_crash)。如何+ Seta Map mp_Crash"标记后添加()部分而不删除它或破坏.exe扩展名?

这是我编写的用于添加参数的代码块:

label1.Text = openFileDialog1.FileName;

shortcut.TargetPath = label1.Text + " Seta Map mp_crash";

shortcut.Save();
Run Code Online (Sandbox Code Playgroud)

这段代码会将seta部分添加到目标中,但会破坏扩展名,看起来像这样 "E:\Cod4\iw3mp.exe Seta Map mp_crash "

请帮忙。这是完整的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IWshRuntimeLibrary;
using System.IO;

namespace WindowsFormsApplication18

{
    public partial class Form1 : Form

    {


        public Form1()

        {

            InitializeComponent( 
            );

        }
        public void CreateShortcut()
        {

            object shDesktop = (object)"Desktop";
            WshShell shell = …
Run Code Online (Sandbox Code Playgroud)

c# parameters shortcut

4
推荐指数
1
解决办法
1908
查看次数

如何在桌面上添加图标到应用程序的快捷方式

我想当用户运行我的c#应用程序时,应用程序将创建一个桌面快捷方式来运行应用程序.我用这个代码:

private void appShortcutToDesktop(string linkName)
{
    string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
    {
        string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
        writer.WriteLine("[InternetShortcut]");
        writer.WriteLine("URL=file:///" + app);
        writer.WriteLine("IconIndex=0");
        string icon = app.Replace('\\', '/');
        writer.WriteLine("IconFile=" + icon);
        writer.Flush();
    }
}

private void button1_Click(object sender, EventArgs e)
{
    appShortcutToDesktop("MyName");
}
Run Code Online (Sandbox Code Playgroud)

此代码创建快捷方式,但我想放置myicon.ico快捷方式图标.我怎样才能做到这一点 ?

c# icons shortcut winforms

2
推荐指数
1
解决办法
4150
查看次数

如何在 C# 中创建相对的 Windows 快捷方式?

我试图在这里合并两个问题:1)如何在 Windows 中创建相对快捷方式和 2)如何 在 C# 中创建 Windows 快捷方式

我有以下代码来创建快捷方式(基于我见过的问题),但在分配时它会抛出异常shortcut.TargetPath:“值不在预期范围内”

public void CreateShortcut() {
    IWshRuntimeLibrary.WshShell wsh = new IWshRuntimeLibrary.WshShell();
    IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(Path.Combine(outputFolder, "sc.lnk")) as IWshRuntimeLibrary.IWshShortcut;
    shortcut.Arguments = "";
    shortcut.TargetPath = "%windir%\\system32\\cmd.exe /c start \"\" \"%CD%\\folder\\executable.exe\"";
    shortcut.WindowStyle = 1;
    shortcut.Description = "executable";
    shortcut.WorkingDirectory = "%CD%";
    shortcut.IconLocation = "";
    shortcut.Save();
}
Run Code Online (Sandbox Code Playgroud)

如何解决此问题并创建相对快捷方式?

注意:我不想编写批处理脚本来执行此操作,因为我的软件很可能会安装在用户无法访问命令行的 PC 上 - 我们的客户通常拥有非常锁定的计算机,并且将几乎可以肯定没有运行批处理文件的正确权限。如果您确实对我如何创建一个“便携式”文件夹(在子文件夹中包含我的 .exe)有任何建议,其中用户只需双击顶级文件夹中的某些内容即可运行 exe,我愿意建议!

c# windows

2
推荐指数
1
解决办法
1438
查看次数

标签 统计

c# ×6

shortcut ×4

winforms ×2

.net-4.0 ×1

automation ×1

c++ ×1

icons ×1

lnk ×1

parameters ×1

windows ×1

windows-shell ×1