繁体   English   中英

尝试使用C#打开PowerPoint文件时引发异常

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

我正在开发我的第一个C#应用程序。 我正在尝试以全屏模式打开PowerPoint文件。 该代码需要cmd参数。 我将powerpoint test.pptm放置在与应用程序输出(调试和发布)相同的文件夹中。 我写了以下代码:

        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
        {
        }

代码会编译,但是当我尝试通过按钮运行此代码时,控制台会指出:

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

我尝试通过更改以下行直接引用pptm文件:

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

我收到一条错误消息,指出Unrecognized escape sequence 有谁之前经历过这个吗? 我已经坚持了一段时间。 谢谢!

在文件路径前面加上@符号

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

从MSDN

逐字字符串文字包含一个@字符,后跟一个双引号字符,零个或多个字符以及一个结束的双引号字符。 一个简单的例子是@“ hello”。 在逐字字符串文字中,定界符之间的字符逐字解释,唯一的例外是引号-转义序列。

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

有关使代码正常工作的一些提示

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. 请注意全路径之前和之后的转义双引号。 这是为了在文件名或目录中容纳空格
  2. 删除行startInfo.UseShellExecute = false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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