[英]Exception thrown when trying to open a PowerPoint file with C#
I am working on my first C# application. 我正在开发我的第一个C#应用程序。 I am trying to open a PowerPoint file in fullscreen mode.
我正在尝试以全屏模式打开PowerPoint文件。 The code requires cmd arguments.
该代码需要cmd参数。 I placed my powerpoint
test.pptm
in the same folder as the output (debug and release) for my application. 我将powerpoint
test.pptm
放置在与应用程序输出(调试和发布)相同的文件夹中。 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: 我尝试通过更改以下行直接引用pptm文件:
startInfo.Arguments = "/sc:\\path\\to\\full\\file\\test.pptm";
I get an error stating Unrecognized escape sequence
. 我收到一条错误消息,指出
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 从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".
一个简单的例子是@“ 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 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""";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.