【问题标题】:Exception thrown when trying to open a PowerPoint file with C#尝试使用 C# 打开 PowerPoint 文件时引发异常
【发布时间】:2015-11-12 18:56:15
【问题描述】:

我正在开发我的第一个 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 = "/s c:\path\to\full\file\test.pptm";

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

【问题讨论】:

  • 你能帮我试试这个吗? startInfo.Arguments = "/s ""c:\path\to\full\file\test.pptm""";
  • @PauloLima 谢谢,让我把完整的路径。但主要问题仍然存在,即Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll。该程序似乎具有所需的正确命令,但无法执行。
  • 看看this post能不能帮到你
  • @PauloLima 该解决方案对我有用。另一种解决方案(本文中的答案之一)也起到了作用。谢谢!

标签: c# powerpoint


【解决方案1】:

在你的文件路径前加上@符号

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

【讨论】:

  • @cubrr 注意。也加入解释
  • 非常感谢!它现在似乎正在工作。问题是startInfo.UseShellExecute = false 行。这导致我的异常错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 2018-06-22
  • 1970-01-01
相关资源
最近更新 更多