只运行一个应用程序的错误代码的分析

错误代码一

//单独单开exe的时候,是没有问题的
//
此代码的问题在于,打开两个或多个独立的VS程序后,并且这些VS打开的项目的名字相同的话,会有多个进程主模块名字相同,但是进程ID不同的程序 导致无法启动实例【一个程序都打不开】 Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); OperationLog.Instance.WriteLog( string.Format("进程Id={2} 进程名字 = {0},进程主模块的名字 = {1}", current.ProcessName, current.MainModule.FileName, current.Id), LogType.UI); OperationLog.Instance.WriteLog(string.Format("进程名字={0}的进程数量 = {0}", processes.Length), LogType.UI); OperationLog.Instance.WriteLog( string.Format("System.Reflection.Assembly.GetExecutingAssembly().Location.Replace(\"/\", \"\\\") = {0}", System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\")), LogType.UI); foreach (Process process in processes) { OperationLog.Instance.WriteLog( string.Format("process.Id = {0},process.MainModule.FileName = {1}", process.Id, process.MainModule.FileName), LogType.UI); if (process.Id != current.Id) { if (process.MainModule.FileName == current.MainModule.FileName) { return process; } } } return null; }

错误代码二

//只打开exe的话,没问题
//
这个虽然解决错误代码一种关于多个VS程序,导致的不能运行程序的问题,【但是会导致,多个VS能打开多个程序】 并且调试运行处exe的时候,还可以从bin目录下直接打开exe Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); OperationLog.Instance.WriteLog( string.Format("进程Id={2} 进程名字 = {0},进程主模块的名字 = {1}", current.ProcessName, current.MainModule.FileName, current.Id), LogType.UI); OperationLog.Instance.WriteLog(string.Format("进程名字={0}的进程数量 = {0}", processes.Length), LogType.UI); OperationLog.Instance.WriteLog( string.Format("System.Reflection.Assembly.GetExecutingAssembly().Location.Replace(\"/\", \"\\\") = {0}", System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\")), LogType.UI); foreach (Process process in processes) { OperationLog.Instance.WriteLog( string.Format("process.Id = {0},process.MainModule.FileName = {1}", process.Id, process.MainModule.FileName), LogType.UI); if (process.Id != current.Id) { OperationLog.Instance.WriteLog(string.Format("System.Reflection.Assembly.GetExecutingAssembly().Location = {0}", System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\")), LogType.UI); if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)//只是区分了调试和非调试,能打开任意多个VS调试程序。并且还可以额外打开exe { return process; } } } OperationLog.Instance.WriteLog(string.Format("{0}", Environment.NewLine+Environment.NewLine), LogType.UI); return null;

 

错误代码三

posted @ 2015-01-08 17:08  ChuckLu  阅读(355)  评论(1编辑  收藏  举报