0
votes

I am working on my first C# application. I am trying to open a PowerPoint file in fullscreen mode. The code requires cmd arguments. I placed my powerpoint test.pptm in the same folder as the output (debug and release) for my application. I have written the following code:

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "powerpnt.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = "/s test.pptm";

        try
        {
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch
        {
        }

The code compiles, but when I try to run this code via a button, the console states:

Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll

I've tried to directly reference the pptm file by changing the following line:

startInfo.Arguments = "/s c:\path\to\full\file\test.pptm";

I get an error stating Unrecognized escape sequence. Has anyone experienced this before? I've been stuck on this for a little while. Thanks!

1
can you try this for me please? startInfo.Arguments = "/s ""c:\path\to\full\file\test.pptm"""; - Paulo Lima
@PauloLima Thanks, that let's me put the full path. But the main problem still persists, that is Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll. The program seems to have the proper command needed, but it can't execute it. - Alex Weber
see if this post can help you - Paulo Lima
@PauloLima That solution worked for me. Another solution (one of the answers in this post) also did the trick. Thanks! - Alex Weber

1 Answers

4
votes

Prefix your file path with the @ sign

startInfo.Arguments = @"/s c:\path\to\full\file\test.pptm";

From MSDN

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence.

https://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

A few pointers regarding your code to get it working

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;            
startInfo.FileName = "powerpnt.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"/s ""fullpath with spaces in file names""";
  1. Notice the escaped double quotes before and after the fullpath. This is to accommodate spaces in your file names or directories
  2. Remove the line startInfo.UseShellExecute = false