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

发布于 2024-11-25 07:06:19 字数 683 浏览 5 评论 0原文

我有一个应用程序,可以在桌面上创建快捷方式,并允许您将文件拖放到快捷方式中以执行操作(将 Word 文档转换为 PDF)。现在我想做的是使用 shellexecute (.NET Process.Start()) 以编程方式执行此操作。

问题是它似乎不起作用,我偷偷怀疑这与创建的快捷方式将“开始于”参数设置为特定文件夹这一事实有关。

所以它看起来像这样:

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

我的代码如下。

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

从根本上来说,我的问题归结为:“Startin”对于快捷方式实际上意味着什么/做什么?在使用 shellexecute 或 Process.Start 启动应用程序时我可以复制此功能吗?

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). Now what I am trying to do is perform this action programmatically using 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宣告ˉ结束 2024-12-02 07:06:19

当您使用Process.Start时,您可以使用ProcessStartInfo调用它,而ProcessStartInfo又恰好能够设置WorkingDirectory属性 - 这样您就可以可以复制这种行为。

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.

ゞ花落谁相伴 2024-12-02 07:06:19

正如 Yahia 所说,设置WorkingDirectory 属性。您还需要引用论点。这是一个粗略的例子:

//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.");
}

As Yahia said, set the WorkingDirectory property. You also need to quote the 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.");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文