服务器是win2003,程序是放在“启动”那里自动启动的,但多个用户远程登录后会造成程序开多个,有什么办法防止程序在windows系统中只开一个? (不做成“服务”的前提下),先谢谢了!!!

解决方案 »

  1.   

    不同用户没试过试过,我有当前用户不能多开的 你试试不同用户行不行。
            #region 程序只运行一次
            /// <summary>
            /// 程序只运行一次
            /// </summary>
            /// <returns>null</returns>
            public static Process RunningInstance()
            {
                Process current = Process.GetCurrentProcess();
                Process[] processes = Process.GetProcessesByName(current.ProcessName);
                //Loop through the running processes in with the same name 
                foreach (Process process in processes)
                {
                    //Ignore the current process 
                    if (process.Id != current.Id)
                    {
                        //Make sure that the process is running from the exe file. 
                        if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                        {
                            //Return the other process instance. 
                            return process;
                        }
                    }
                }
                //No other instance was found, return null. 
                return null;
            } 
            #endregion
      

  2.   

    using System;
    using System.Threading;namespace ConsoleApplication2
    {
        class Program
        {
            static void Main()
            {
                String strMutexName = "#%#$^%@#$%#%@#@#%#@$%#$^$#";
                Mutex mutex = null;
                try
                {
                    mutex = Mutex.OpenExisting(strMutexName); 
                    Console.WriteLine("已经运行");
                }
                catch
                {
                    mutex = new Mutex(false, strMutexName);          
                }            Console.ReadKey();
            }
        }
    }
      

  3.   


    namespace Com.Xhdz.XD1002.Server
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main(string[] Args)
            {
                //Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);            //只能启动一个实例
                bool createdNew; //返回是否赋予了使用线程的互斥体初始所属权
                System.Threading.Mutex instance = new System.Threading.Mutex(true, "xd1002MutexName", out createdNew); //同步基元变量
                if (!createdNew) //赋予了线程初始所属权,也就是首次使用互斥体
                {
                    MessageBox.Show("已经启动了一个程序,请先退出!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }
                else
                {
                    //获得当前登录的Windows用户标示
                    System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
                    System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
                    //判断当前登录用户是否为管理员
                    if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
                    {
                        //如果是管理员,则直接运行
                        Application.Run(new MainForm());
                    }
                    else
                    {
                        //创建启动对象
                        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                        //设置运行文件
                        startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
                        //设置启动参数
                        startInfo.Arguments = String.Join(" ", Args);
                        //设置启动动作,确保以管理员身份运行
                        startInfo.Verb = "runas";
                        //如果不是管理员,则启动UAC
                        System.Diagnostics.Process.Start(startInfo);
                        //退出
                        System.Windows.Forms.Application.Exit();
                    }
                }
            }
        }
    }