C#ShortCut路径修改

时间:2013-08-28 14:40:47

标签: c# parameters shortcut

我创建了一个程序,使用某个库生成通过打开文件对话框选择的特定EXE的快捷方式。我让它工作,但我希望程序向Target路径添加一个参数,使其如下所示:("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();

此代码会将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 = new WshShell();
            string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Server.lnk";
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
            shortcut.Description = "Server Shortcut";
            shortcut.Hotkey = "Ctrl+Shift+N";
            var ofd = new OpenFileDialog();
            ofd.ShowDialog();
            shortcut.TargetPath = '"' + ofd.FileName + '"' + "+Seta Map mp_crash";
        }

        private void button1_Click(object sender, EventArgs e)
        {

            CreateShortcut();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          //  var ofd = new OpenFileDialog();
         //   ofd.ShowDialog();
        //    string shortcut = '"' + ofd.FileName + '"' + "+Seta Map mp_crash";
         //   openFileDialog1.DefaultExt = "EXE";
      //  / //  openFileDialog1.FileName = "Iw3mp.exe";
         //  DialogResult result2 = openFileDialog1.ShowDialog();
       //   label1.Text = openFileDialog1.FileName;
       //   a = label1.Text;

        //    if (result2 == DialogResult.OK) 
        //   {
        //    }
        }
    }
}

3 个答案:

答案 0 :(得分:5)

根据您更新的问题,试试这个

shortcut.TargetPath = ofd.FileName;
shortcut.Arguments = "Seta Map mp_crash";

答案 1 :(得分:1)

这是你想要做的吗?

        var ofd = new OpenFileDialog();
        ofd.ShowDialog();
        string shortcut = '"' + ofd.FileName + '"' + " +Seta Map mp_crash";

应格式化字符串的方式......

答案 2 :(得分:1)

感谢每个人花时间去发现我不能特别的KeyboardP和他的工作代码,而不是

shortcut.TargetPath = ofd.FileName;
shortcut.Arguments = "Seta Map mp_crash";