【发布时间】:2015-02-02 10:03:15
【问题描述】:
我试着这样做:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using DannyGeneral;
namespace mws
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
if (IsApplicationAlreadyRunning() == true)
{
MessageBox.Show("The application is already running");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
catch (Exception err)
{
Logger.Write("error " + err.ToString());
}
}
static bool IsApplicationAlreadyRunning()
{
string proc = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(proc);
if (processes.Length > 1)
{
return true;
}
else
{
return false;
}
}
}
}
但是我遇到了一些问题。
首先,当我在 Visual Studio 中加载我的项目然后运行我的程序时,它会检测我的项目的 vshost.exe 文件,例如:My project.vshost
并且我希望它仅在我运行程序时才检测我的程序是否在运行,例如只有当它找到 .exe 时:我的 project.exe 而不是 vshost。
【问题讨论】:
-
您可以尝试禁用Visual Studio托管进程,在这种情况下,该进程不会命名为vshost
-
那么你真正想要的是检测你是否在“vshost”中运行?