在右键单击pdf文件后,从上下文菜单运行该程序,它只需添加" \ CALL OFF"对于所选文件,本地程序工作正常,即使有空格。当我在我的nas上的文件中运行路径包含空格时,GetCommandLineArgs的输出在第一个空格处停止。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
textBox1.Text = args[1];
}
private void button2_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = "-add-text \"/ CALL OFF\" -font \"Helvetica-Bold\" -font-size 14 -pos-left \"194 776\" " + textBox1.Text + " -o out.pdf";
start.FileName = "cpdf";
Process.Start(start);
}
}
答案 0 :(得分:1)
String Join所有命令行参数以空格作为分隔符返回到一个字符串中,并将其用作 参数。在textBox1.Text
中使用时,您还需要在start.Arguments
周围添加双引号,以确保将其作为一个接收:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs().Skip(1).ToArray();
textBox1.Text = String.Join(" ", args);
}
private void button2_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = "-add-text \"/ CALL OFF\" -font \"Helvetica-Bold\" -font-size 14 -pos-left \"194 776\" "
+ "\"" + textBox1.Text + "\"" + " -o out.pdf";
start.FileName = "cpdf";
Process.Start(start);
}
}
答案 1 :(得分:0)
您需要将路径名括在引号中,然后添加\"
:
string quoted = "\"" + textBox1.Text + "\"";
得到:
start.Arguments = "-add-text \"/ CALL OFF\" -font \"Helvetica-Bold\" -font-size 14 -pos-left \"194 776\" \"" + textBox1.Text + "\" -o out.pdf";
如果您将它作为命令行参数传递给程序,那么您还需要在那里引用它:
myExe -file "Long path with spaces\file.pdf"