简体   繁体   English

启动进程时以编程方式设置起始位置

[英]Programmatically setting startin location when starting a process

I have an application that creates a shortcut on my desktop and allows you to drag and drop files into the shortcut to perform an action (convert a word document to PDF).我有一个应用程序可以在我的桌面上创建一个快捷方式,并允许您将文件拖放到快捷方式中以执行操作(将 word 文档转换为 PDF)。 Now what I am trying to do is perform this action programmatically using shellexecute (.NET Process.Start()).现在我要做的是使用 shellexecute (.NET Process.Start()) 以编程方式执行此操作。

The problem is that it doesnt seem to be working and I have a sneaking suspicion this has something to do with the fact that the shortcut created has the "Start in" parameter set to a specific folder.问题是它似乎没有工作,我有一个偷偷摸摸的怀疑这与创建的快捷方式将“开始”参数设置为特定文件夹的事实有关。

So it looks like this:所以它看起来像这样:

Shortcut target: "C:\Program Files (x86)\MyPDFConvertor\MyPDFConvertor.exe"
Shortcut startin: "C:\Program Files (x86)\MyPDFConvertor\SomeSubfolder\SomeSubSubFolder"

My code was the following.我的代码如下。

System.Diagnostics.Process.Start("C:\\Program Files (x86)\\MyPDFConvertor\\MyPDFConvertor.exe", "C:\\MyFiles\\This is a test word document.docx");

Fundamentally my question boils down to: What does "Startin" actually mean/do for shortcuts and can I replicate this functionality when starting an application using either shellexecute or Process.Start?从根本上说,我的问题归结为:“Startin”对于快捷方式实际上意味着/做什么,我可以在使用 shellexecute 或 Process.Start 启动应用程序时复制此功能吗?

When you use Process.Start you can call it with a ProcessStartInfo which in turn happens to be able to setup a WorkingDirectory property - this way you can replicate that behaviour.当您使用Process.Start时,您可以使用ProcessStartInfo调用它,而后者恰好能够设置WorkingDirectory属性 - 这样您就可以复制该行为。

As Yahia said, set the WorkingDirectory property.正如 Yahia 所说,设置 WorkingDirectory 属性。 You also need to quote the arguments.您还需要引用 arguments。 Here is a rough example:这是一个粗略的例子:

//System.Diagnostics.Process.Start("C:\\Program Files (x86)\\MyPDFConvertor\\MyPDFConvertor.exe", "C:\\MyFiles\\This is a test word document.docx");
ProcessStartInfo start = new ProcessStartInfo();
//must exist, and be fully qualified:
start.FileName = Path.GetFullPath("C:\\Program Files (x86)\\MyPDFConvertor\\MyPDFConvertor.exe");
//set working directory:
start.WorkingDirectory = Path.GetFullPath("C:\Program Files (x86)\MyPDFConvertor\SomeSubfolder\SomeSubSubFolder");
//arguments must be quoted:
const char quote = '"';
start.Arguments = quote + "C:\\MyFiles\\This is a test word document.docx" + quote;
//disable the error dialog
start.ErrorDialog = false;
try
{
    Process process = Process.Start(start);
    if(process == null)
    {//started but we don't have access

    }
    else
    {
        process.WaitForExit();
        int exitCode = process.ExitCode;
    }
}
catch
{
    Console.WriteLine("failed to start the program.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM