2013-08-28 123 views
4

我創建了一個程序,它使用某個庫生成通過打開文件對話框選擇的特定EXE的快捷方式。我得到它的工作,但我想要程序添加一個參數到目標路徑,使其看起來像這樣:("E:\Cod4\iw3mp.exe" +Seta Map mp_crash)。我能做些什麼來在"標記之後添加(+ Seta Map mp_Crash)部分而不刪除它或破壞.exe的擴展名?C#快捷方式路徑修改

這裏是代碼塊,我寫添加參數:

label1.Text = openFileDialog1.FileName; 

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

shortcut.Save(); 

此代碼將在剛毛部件添加到目標,但它會毀了擴展,它看起來像這樣"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) 
     // { 
     // } 
     } 
    } 
} 

回答

4

,試試這個

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

WindowsFormsApplication18.exe中出現未處理的「System.ArgumentException」類型異常 其他信息:值不在預期範圍內。 – VoVb

+0

什麼行會拋出該錯誤?上面的代碼只是返回一個帶有'label1.Text'和命令行開關值的引號的字符串。您可以在'{1}'之前添加'-'來分隔參數。 – keyboardP

+0

shortcut.targetpath行 – VoVb

1

難道這就是你想幹什麼?

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

應該格式化你想要的字符串......

+0

相同的錯誤在WindowsFormsApplication18.exe中發生未處理的類型'System.ArgumentException'異常 附加信息:值不在預期的範圍內 – VoVb

1

感謝名單每一個花時間去發現我不能特別KeyboardP和他的工作代碼,感謝名單

shortcut.TargetPath = ofd.FileName; 
shortcut.Arguments = "Seta Map mp_crash"; 
相關問題