簡體   English   中英

如何將帶有 %20 的文件路徑作為參數傳遞給進程?

[英]How can I pass a file path with %20 as an argument to a process?

我目前正在使用 C# 開發一個應用程序,該應用程序將使用給定的參數(應用程序路徑和我們試圖打開的文檔)啟動一個應用程序。

這是我到目前為止嘗試過的代碼:

var pi = new ProcessStartInfo(filePath)
{
        Arguments = "\"" + Path.GetFileName(filePath) + "\"",
        UseShellExecute = false,
        WorkingDirectory = Path.GetDirectoryName(filePath),
        FileName = appPath,
        Verb = "OPEN"
};
Process.Start(pi);

其中filePath是我們要打開的文件的路徑, appPath是我們要在其中打開文件的應用程序的路徑 ( C:\\Program Files\\...\\POWERPNT.exe )。

此解決方案適用於帶空格和不帶空格的文件,但不適用於帶有“%20”的文件,這些文件拒絕在 PowerPoint 等應用程序中打開。 下面的例子:

“PowerPoint 無法打開這種類型的文件 (C:\\...\\...\\Statistics Made Easy.ppt)。”

在 Windows 資源管理器上,文件名為 Statistics%20Made%20Easy.ppt。 請注意,錯誤消息中的 %20 已被空格替換。 可能是什么問題?

一種解決方案是不設置工作目錄,而是使用文件名的完整路徑:

var pi = new ProcessStartInfo(filePath)
{
        Arguments = "/ou \"" + filePath + "\"",                 // full path here
        UseShellExecute = false,
        // WorkingDirectory = Path.GetDirectoryName(filePath),  // skip this
        FileName = appPath,
        Verb = "OPEN"
};

如果 Powerpoint 是打開 PPT 文件的默認應用程序,您還可以使用

Process.Start(filePath);

根本沒有指定pi 如果用戶更改了要與 PPT 文件一起使用的應用程序,當然這可能會啟動一個不同的應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM