How can I open files with spaces on Microsoft Office apps via C#?

I'm currently using C# to develop an app that will launch an app with given parameters (the app path and the document we're trying to open).

Here's the code I've tried so far:

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

where filePath is the path to the file we want to open and appPath is the path of the app we want to open the file in (C:\Program Files...\POWERPNT.exe).

This solution works for files without spaces, however it doesn't work for files with '%20' characters, which refuse to open in apps like PowerPoint. Example below:

"PowerPoint can't open this type of file (C:\...\...\Statistics Made Easy.ppt)."

On Windows Explorer, the name of the file is Statistics%20Made%20Easy.ppt - what could the issue be?



Read more here: https://stackoverflow.com/questions/64896583/how-can-i-open-files-with-spaces-on-microsoft-office-apps-via-c

Content Attribution

This content was originally published by Nitish Vijai at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: