c# - p.Start 中的 C# 错误

标签 c# vb.net visual-studio process vb.net-to-c#

我最近开始将一个项目从 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/

相关文章:

c# - 如何将文件 (PDF) 插入到 varbinary SQL Server 列中并稍后检索它?

json - 当我想存储 UTC 日期时间时,如何停止 Entity Framework 转换 Json UTC 日期时间

c# - 在 C# 中处理静态字段内存泄漏的最佳实践

c# - 开发 Multi-Tenancy asp.net MVC 应用程序时要记住什么?

vb.net - "Conversion from string ""to type ' 双 ' is not valid"

c++ - Qt 移动构造函数链接错误

c# - 设计者未保存基类的属性值

ios - 如何从终端更新 Visual Studio for Mac/Xamarin Studio?

c# - 从 DataGridView 创建图表

arrays - 如何与 VB.Net 中的常量集合进行比较?