错误试图从PowerShell中运行C#:在

问题描述:

试图赶上无效令牌“使用”当屏幕被锁定,发现C#是如此,但每次我试图将其纳入PowerShell中显然完成一个片段,我得到一对夫妇不同的错误:错误试图从PowerShell中运行C#:在

$Assemblies = @('System', 'System.Messaging') 

$type = @" 
using System; 
using System.Messaging; 

public class Win32Session 
{ 
    private const int NOTIFY_FOR_THIS_SESSION = 0; 
    private const int WM_WTSSESSION_CHANGE = 0x2b1; 
    private const int WTS_SESSION_LOCK = 0x7; 
    private const int WTS_SESSION_UNLOCK = 0x8; 

    public event EventHandler MachineLocked; 
    public event EventHandler MachineUnlocked; 

    public Win32Session() 
    { 
     ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage; 
    } 

    void ComponentDispatcher_ThreadFilterMessage(ref MSG msg, ref bool handled) 
    { 
     if (msg.message == WM_WTSSESSION_CHANGE) 
     { 
      int value = msg.wParam.ToInt32(); 
      if (value == WTS_SESSION_LOCK) 
      { 
       OnMachineLocked(EventArgs.Empty); 
      } 
      else if (value == WTS_SESSION_UNLOCK) 
      { 
       OnMachineUnlocked(EventArgs.Empty); 
      } 
     } 
    } 

    protected virtual void OnMachineLocked(EventArgs e) 
    { 
     EventHandler temp = MachineLocked; 
     if (temp != null) 
     { 
      temp(this, e); 
     } 
    } 

    protected virtual void OnMachineUnlocked(EventArgs e) 
    { 
     EventHandler temp = MachineUnlocked; 
     if (temp != null) 
     { 
      temp(this, e); 
     } 
    } 
} 
"@ 

Add-Type -MemberDefinition $type -Name 'Eventing' -ReferencedAssemblies $Assemblies 

...如果我留在下面的字符串的“使用”的语句,我得到“使用”是一个无效的令牌错误。如果我删除'using'语句并重新运行,编译器会抱怨不知道MSG类型是什么。任何人都可以协助谢谢!

+0

的usings是无效的(这就是'-ReferencedAssemblies'是有)。我也不知道MSG类型是什么 - 它没有在任何引用的程序集中定义。 – Adrian

+0

@Adrian引用程序集与向.cs文件添加'using'指令不同。 –

+0

@EdPlunkett是真的,尽管这是一个PowerShell文件,而不是C#文件。我已经添加了一些更好的信息。 – Adrian

您可以使用-UsingNamespace parameter来达到using指令在C#文件中的目的。但是,MSG类型在引用的命名空间中似乎未定义。