问题
I've created a program which generates a shortcut to a specific EXE selected via the open file dialog, using some library. I got it to work but I want the program to add a parameter to the Target path to make it look like this: ("E:\Cod4\iw3mp.exe" +Seta Map mp_crash
). What can I do to add the (+ Seta Map mp_Crash
) part after the "
mark without removing it or ruining the extension of the .exe?
Here is block of the code I wrote to add the parameter:
label1.Text = openFileDialog1.FileName;
shortcut.TargetPath = label1.Text + " Seta Map mp_crash";
shortcut.Save();
This code will add the seta part to the target but it will ruin the extension and it will look like this "E:\Cod4\iw3mp.exe Seta Map mp_crash "
Please help. here is the full code :
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)
// {
// }
}
}
}
回答1:
Based on your updated question, try this
shortcut.TargetPath = ofd.FileName;
shortcut.Arguments = "Seta Map mp_crash";
回答2:
Is this what you are trying to do?
var ofd = new OpenFileDialog();
ofd.ShowDialog();
string shortcut = '"' + ofd.FileName + '"' + " +Seta Map mp_crash";
that should format the string how you want it...
回答3:
thanx every one for spending time to discover what i couldn't especially KeyboardP and his working code , thanx
shortcut.TargetPath = ofd.FileName;
shortcut.Arguments = "Seta Map mp_crash";
来源:https://stackoverflow.com/questions/18491076/c-sharp-shortcut-path-modification