我做了一个软件,C#写的,现在我不想让
别人用外部软件启动,只能用鼠标启动软件 ,应该怎么办呢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication1
{
public static class ProcessExtensions
{
private static string FindIndexedProcessName(int pid)
{
var processName = Process.GetProcessById(pid).ProcessName;
var processesByName = Process.GetProcessesByName(processName);
string processIndexdName = null;
for (var index = 0; index < processesByName.Length; index++)
{
processIndexdName = index == 0 ? processName : processName + "#" + index;
var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
if ((int)processId.NextValue() == pid)
{
return processIndexdName;
}
}
return processIndexdName;
}
private static Process FindPidFromIndexedProcessName(string indexedProcessName)
{
var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
return Process.GetProcessById((int)parentId.NextValue());
}
public static Process Parent(this Process process)
{
return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Process.GetCurrentProcess().Parent().ProcessName);
}
}
}
用如上代码,如果Process.GetCurrentProcess().Parent().ProcessName不是Exporer.exe,那么就是别的程序启动的。
非常感谢你的回答
那如果 这个外部软件名也起了个exporer.exe,应该怎么解啊
ProcessName是包含全路径的,我想谁敢把自己的软件起名exporer.exe并且把系统原有的同名文件给覆盖了?