我最近开始将一个项目从 VB 移植到 C#,所以如果这是一个菜鸟问题,我深表歉意。我似乎无法让这个过程在我的一生中发挥作用:
Process p = default(Process);
try
{
p.Start("powershell",
"-ExecutionPolicy ByPass -windowstyle hidden -file .\\scripts\Excel.ps1");
}
catch(Exception ex)
{
}
我收到几个错误:
Unrecognized escape sequence
Member Process.Start(string, string) cannot be accessed with an instance reference; qualify it with a type name instead.
如有任何帮助,我们将不胜感激。
编辑:我从 VB.net 迁移到 C#,因为我更喜欢 C# 的语法(来自 Java 背景),但我不了解大量 C# Visual Studio 工作原理。
编辑2:用@修复了引号,仍然得到
- Member Process.Start(string, string) cannot be accessed with an instance reference; qualify it with a type name instead
错误。
最佳答案
您需要在逐字字符串中使用双斜杠
p.Start("powershell", "-ExecutionPolicy ByPass -windowstyle hidden -file .\\scripts\\Excel.ps1");
另一个选项是在其前面使用@符号。
对于第二个错误:
错误消息显示:Member Process.Start(string, string) 无法通过实例引用访问;而是用类型名称来限定它。所以你需要像静态方法
一样调用它:
System.Diagnostics.Process.Start( "powershell", "-ExecutionPolicy ByPass -windowstyle hidden -file .\\scripts\\Excel.ps1");
关于c# - p.Start 中的 C# 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32265572/