以下是Program.cs的代码 static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Simulate sim = new Simulate(); Process current = Process.GetCurrentProcess(); bool newinstance = true; Process[] processes = Process.GetProcessesByName(current.ProcessName); //遍历正在有相同名字运行的例程 foreach (Process process in processes) { //忽略现有的例程 if (process.Id != current.Id) { //确保例程从EXE文件运行 if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { //返回另一个例程实例 current = process; newinstance = false; break; } } } if (newinstance) { Application.Run(sim); } else { ShowWindowAsync(current.MainWindowHandle, 1); //设置真实例程为foreground window SetForegroundWindow(current.MainWindowHandle); } } [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); }
Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) E:\wwwroot\newcaigou\WindowsApplication1\Program.cs 56 10 WindowsApplication1 Error 2 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) E:\wwwroot\newcaigou\WindowsApplication1\Program.cs 59 10 WindowsApplication1
似乎要加哪个 using ?
using System.Diagnostics; using System.Runtime.InteropServices; using System.Reflection;
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Simulate sim = new Simulate();
Process current = Process.GetCurrentProcess();
bool newinstance = true;
Process[] processes = Process.GetProcessesByName(current.ProcessName); //遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//返回另一个例程实例
current = process;
newinstance = false;
break;
}
}
}
if (newinstance)
{
Application.Run(sim);
}
else
{
ShowWindowAsync(current.MainWindowHandle, 1); //设置真实例程为foreground window
SetForegroundWindow(current.MainWindowHandle);
}
} [DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}
Error 2 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) E:\wwwroot\newcaigou\WindowsApplication1\Program.cs 59 10 WindowsApplication1
using System.Runtime.InteropServices;
using System.Reflection;