尝试使用 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 = "/s c:\path\to\full\file\test.pptm";
我收到一条错误消息 Unrecognized escape sequence
。有谁之前经历过这个吗?我已经坚持了一段时间。谢谢!
在您的文件路径前加上 @ 符号
startInfo.Arguments = @"/s c:\path\to\full\file\test.pptm";
来自 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
关于您的代码以使其正常工作的一些提示
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "powerpnt.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"/s ""fullpath with spaces in file names""";
- 注意完整路径前后的转义双引号。这是为了在您的文件名或目录中容纳空格
- 删除行 startInfo.UseShellExecute = false
我正在开发我的第一个 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";
来自 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
关于您的代码以使其正常工作的一些提示
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "powerpnt.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"/s ""fullpath with spaces in file names""";
- 注意完整路径前后的转义双引号。这是为了在您的文件名或目录中容纳空格
- 删除行 startInfo.UseShellExecute = false