简体   繁体   中英

Exception thrown when trying to open a PowerPoint file with C#

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 = "/sc:\\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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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